Home >Web Front-end >JS Tutorial >5 Tips for More Efficient jQuery Selectors
The core of jQuery lies in querying. It uses the CSS selector syntax to find DOM elements and execute methods on these elements collections. jQuery uses native browser API methods to retrieve DOM collections. Newer browsers support getElementsByClassName
, querySelector
and querySelectorAll
, which can parse CSS syntax. However, older browsers only offer getElementById
and getElementByTagName
. In the worst case, jQuery's Sizzle engine must parse the selector string and look for matching elements. Here are five tips to help you optimize your jQuery selector:
HTML ID attributes are unique in each page, and even older browsers can locate individual elements very quickly:
$("#myelement");
The following class selectors run very fast in modern browsers:
$(".myclass");
But in older browsers like IE6/7 and Firefox 2, jQuery must check every element on the page to determine if "myclass" is applied. If we qualify it with a tag name, the selector will be more efficient, for example:
$("div.myclass");
jQuery can now limit searches to DIV elements.
Avoid overly complex CSS selectors. Unless your HTML document is extremely complex, it is rare to require more than two or three qualifiers. Consider the following complex selectors:
$("body #page:first-child article.main p#intro em");
p#intro
must be unique, so the selector can be simplified to:
$("p#intro em");
It is useful to understand how jQuery's selector engine works. It works from the last selector, so in older browsers, a query like this:
$("p#intro em");
will load all em
elements into an array. It then iterates upwards through the parent node of each node and rejects those nodes that cannot be found. If there are hundreds of p#intro
tags on the page, the query will be particularly inefficient. Depending on your documentation, you can optimize the query by first retrieving the most qualified selector. It can then be used as the starting point for the subselector, for example: em
$("em", $("p#intro")); // 或 $("p#intro").find("em");
$("p").css("color", "blue"); $("p").css("font-size", "1.2em"); $("p").text("Text changed!");Remember jQuery supports chain operations; multiple methods can be applied to the same collection. Therefore, the same code can be rewritten to apply only to a single selector:
$("p").css({ "color": "blue", "font-size": "1.2em"}).text("Text changed!");If you need to use the same set of elements multiple times, you should cache the jQuery object in a variable, for example:
$("#myelement");
Unlike standard DOM collections, jQuery collections are not dynamic and objects are not updated when paragraph tags are added or removed from the document. You can solve this limitation by creating a DOM collection and passing it to the jQuery function when needed, for example:
$(".myclass");
Do you have other jQuery selector optimization tips?
Frequently Asked Questions about Efficient jQuery Selectors (FAQ)
(The FAQ part is omitted here because it is highly duplicated with the original FAQ content, pseudo-originality is difficult and too long. Some FAQ problems can be optionally retained or rewritten as needed.)
The above is the detailed content of 5 Tips for More Efficient jQuery Selectors. For more information, please follow other related articles on the PHP Chinese website!