The Selectors API allows us to utilize CSS (1-3) selectors to collect nodes from the DOM. This is actually quite a common enhancement in a lot of Javascript libraries....CSS selectors are a very efficient and powerful way to quickly look up nodes, and since most people are familiar with CSS syntax, it is very user friendly. The Selectors API offers native browser support for CSS selectors using the querySelector and querySelectorAll methods.
The querySelector method as defined by the W3C returns the first element matching the selector, or if no matching element is found, it returns a null value.
The querySelectorAll method returns a StaticNodeList of all elements matching the selector, or if no matching elements were found, it returns a null value. For anyone familiar with DOM traversal, you are probably familiar with NodeLists. NodeLists are returned by methods like getElementsByTagName. The main difference between the StaticNodeList and a NodeList, is that if you remove an element from the document, a NodeList is also affected and therefore the indexes of the NodeList are altered. A StaticNodeList, however, is not affected...hence the Static part.
The querySelector and querySelector methods are very easily used:
- //returns all elements with an error class
- document.querySelectorAll(".error");
- //returns the first paragraph with an error class
- document.querySelector("p.error");
- //returns every other row of a table with an id of data
- document.querySelectorAll("#data:nth-child(even)";
In addition to calling the methods with a single selector, you can also pass groups of selectors seperated by commas, like so:
- document.querySelectorAll(".error, .warning");
- document.querySelector(".error, .warning");
The first line above would return all elements with a class or error or a class of warning. The second line would return the first element with a class of either error or warning.
You can see the advantage of having native support for the SelectorAPI by taking a look at some test results. SlickSpeed runs the test cases using the popular Javascript libraries Prototype, JQuery and ext as well as by using the Selectors API and the results are substantially quicker using the Selectors API. To run the native support test, you will need to go grab the WebKit nightly build. If you don't want to do that, Robert Biggs ran the test in various browsers and has the test results up.
No comments:
Post a Comment