[转] 5个关于提高 jQuery 选择器的小技巧

原文地址:
5 Tips for More Efficient jQuery Selectors
[url]http://www.sitepoint.com/efficient-jquery-selectors/[/url]

As the name implies, jQuery focuses on queries. The core of the library allows you to find DOM elements using CSS selector syntax and run methods on that collection.

jQuery uses native browser API methods to retrieve DOM collections. Newer browsers support getElementsByClassName, querySelector and querySelectorAll which parses CSS syntax. However, older browsers only offer getElementById and getElementByTagName. In the worst scenarios, jQuery’s Sizzle engine must parse the selector string and hunt for matching elements.

Here are five tips which could help you optimize your jQuery selectors…

[b]1. Use an ID if Possible[/b]

HTML ID attributes are unique in every page and even older browsers can locate a single element very quickly:
$("#myelement");

[b]2. Avoid Selecting by Class Only[/b]

The following class selector will run quickly in modern browsers:

$(".myclass");

Unfortunately, in older browser such as IE6/7 and Firefox 2, jQuery must examine every element on the page to determine whether “myclass” has been applied.

The selector will be more efficient if we qualify it with a tag name, e.g.

$("div.myclass");

jQuery can now restrict the search to DIV elements only.

[b]3. Keep it Simple![/b]

Avoid overly complex selectors. Unless you have an incredibly complex HTML document, it’s rare you’ll need any more than two or three qualifiers.

Consider the following complex selector:

$("body #page:first-child article.main p#intro em");

p#intro must be unique so the selector can be simplified:

$("p#intro em");

[b]4. Increase Specificity from Left to Right[/b]

A little knowledge of jQuery’s selector engine is useful. It works from the last selector first so, in older browsers, a query such as:

$("p#intro em");

loads every em element into an array. It then works up the parents of each node and rejects those where p#intro cannot be found. The query will be particularly inefficient if you have hundreds of em tags on the page.

Depending on your document, the query can be optimized by retrieving the best-qualified selector first. It can then be used as a starting point for child selectors, e.g.

$("em", $("p#intro")); // or
$("p#intro").find("em");

[b]5. Avoid Selector Repetition[/b]

It’s rarely necessary to use the same selector twice. The following code selects every p tag three times:

$("p").css("color", "blue");
$("p").css("font-size", "1.2em");
$("p").text("Text changed!");

Remember jQuery offers chaining; multiple methods can be applied to the same collection. Therefore, the same code can be re-written so it applies to a single selector:

$("p").css({ "color": "blue", "font-size": "1.2em"}).text("Text changed!");

You should cache the jQuery object in a variable if you need to use the same set of elements multiple times, e.g.

var $p = $("p");
$p.css("color", "blue");
$p.text("Text changed!");

Unlike standard DOM collections, jQuery collections aren’t live and the object is not updated when paragraph tags are added or removed from the document. You can code around this restriction by creating a DOM collection and passing it to the jQuery function when it’s required, e.g.

var p = document.getElementByTagName("p");
$(p).css("color", "blue");
// update the DOM
$(p).text("Text changed!");

Do you have any further jQuery selector optimization tips?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值