Home > Web Front-end > JS Tutorial > Detailed explanation of usage examples of wrapper set get() method and index() method in jQuery

Detailed explanation of usage examples of wrapper set get() method and index() method in jQuery

伊谢尔伦
Release: 2017-06-19 14:19:49
Original
1962 people have browsed it

jquery allows the packaged set to be processed as a javascript array. You can use simple array subscripts, that is, to obtain any element in the packaged sequence by position.

For example, to get the first element from the set of all elements on the page with the alt attribute, you can write:

$('img[alt]')[0]
Copy after login

If you prefer to use the method Instead of array subscripts, you can use the get() method defined by jquery to achieve your goal.

get syntax: get(index)

Get one or all matching elements in the packaging set. If no parameters are specified, all elements in the package set will be returned in the form of a JavaScript array; if a subscript parameter is specified, the element corresponding to the subscript will be returned.

$("img[alt]").get(0)
Copy after login

Equivalent to the previous example of using array subscripts, that is: $("img[alt]")[0]. The get() method can also be used to convert the element packaging set into an ordinary javascript array. Think:

var allLabeledButtons = $("lable+button").get();
Copy after login

This statement wraps all the

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