Home > Web Front-end > JS Tutorial > Usage of push() in jquery (adding elements to array)_jquery

Usage of push() in jquery (adding elements to array)_jquery

WBOY
Release: 2016-05-16 16:30:21
Original
2684 people have browsed it

push definition and usage

The

push() method adds one or more elements to the end of the array and returns the new length.

Grammar

arrayObject.push(newelement1,newelement2,....,newelementX)

Parameter Description
newelement1 required. The first element to be added to the array.
newelement2 Optional. The second element to be added to the array.
newelementX Optional. Multiple elements can be added.


Return value

Add the specified value to the new length of the array.

Description

The push() method adds its parameters sequentially to the end of arrayObject. It directly modifies the arrayObject instead of creating a new array. The push() method and pop() method use the first-in-last-pop function provided by the array.

Tips and Notes

Note: This method will change the length of the array.
Tip: To add one or more elements to the beginning of an array, use the unshift() method.

Example

In this example we will create an array and change its length by adding an element:

Copy code The code is as follows:

var arr = new Array(3);
arr[0] = "George" ;
arr[1] = "John" ;
arr[2] = "Thomas" ;
document.write(arr "") document.write(arr.push("James") "") document.write(arr);

Output:
George,John,Thomas
4
George,John,Thomas,James

A usage on the Internet:

Copy code The code is as follows:

$(function(){
var buf = [];
buf.push('');
buf.push('');
buf.push('<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>');
$('.footer-banner').html(buf.join(''));

Note: You need to load jquery.js first before using it

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template