jQuery is a popular JavaScript library that makes working with HTML documents and DOM elements simpler, faster, and easier to maintain. Its selectors and operation methods allow developers to quickly access and operate elements on the page, improving developers' development efficiency. In development, we often need to process a batch of elements on the page, such as modifying their attributes, adding or deleting nodes, etc. At this time, we need to use jQuery collection traversal to implement batch operations. So, how to traverse jQuery collection?
jQuery collection is an array composed of DOM element objects, which can be obtained through various selectors. As follows:
var $list = $('ul li'); // 获取所有ul下的li元素
After getting a jQuery object, we can use some methods provided by jQuery to traverse the collection to achieve the effect we want. The following are some commonly used jQuery collection traversal methods:
each()
each()
method is used to traverse Array or object, it can iterate the $list
collection obtained above and execute the specified function on each element.
Syntax:
$.each(array, function(index, value) { // 处理函数体 });
Sample code:
$list.each(function(index) { console.log(index + ": " + $(this).text()); });
Among them, the parameter of the each()
method is a function, which will be used in each() Executed on each element traversed in the method. This function provides two parameters:
-
#index
: the index of the current element. -
value
: The value of the current element.
map()
map()
method can also be used to traverse an array, it will Convert an array into another array and return a new array after executing the function.
Syntax:
$.map(array, function(value, indexOrkey) { // 处理函数体 });
Sample code:
var newArr = $list.map(function(index) { return $(this).text(); }); console.log(newArr);
filter()
filter The ()
method is used to filter elements in the collection that meet specified conditions. It can determine whether to include the element in the new collection based on the value returned by the callback function.
Syntax:
$(selector).filter(function(index){ // 处理函数体 });
Sample code:
var $filtered = $list.filter(function(index) { return $(this).hasClass('active'); }); console.log($filtered);
filter()
The method will return elements that meet the conditions. These elements can be the same as the original collection The element in the same object, or it can be a new jQuery object.
#not()
not()
method excludes the specified element in the collection and returns a value that does not contain the specified element new collection.
Syntax:
$(selector).not(selector);
Sample code:
var $notFiltered = $list.not('.disabled'); console.log($notFiltered);
not()
The method can also accept a function as a parameter, which will be run on each On the element, if the returned value is true, then the element will be included in the new collection.
var $notFiltered = $list.not(function(index) { return $(this).hasClass('active'); }); console.log($notFiltered);
find()
find()
method filters all descendant elements in the current collection and returns matching specified The selector's collection of elements.
Syntax:
$(selector).find(selector);
Sample code:
var $found = $list.find('a'); console.log($found);
In these examples, we see that jQuery provides multiple methods to iterate over collections, which makes it easier to process a jQuery collection. , writing code is simpler. Since these methods are inherited from Array.prototype
and Object.prototype
, they work in jQuery the same way as native JavaScript. So once you master them, you can not only work with collections on jQuery, but you can also use them to operate in native JavaScript.
The above is the detailed content of How to traverse jquery collection. For more information, please follow other related articles on the PHP Chinese website!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor
