module dom {
[Supplemental, NoInterfaceObject]
interface NodeSelector {
Element querySelector(in DOMString selectors);
NodeList querySelectorAll(in DOMString selectors);
};
Document implements NodeSelector;
DocumentFragment implements NodeSelector;
Element implements NodeSelector; . That is, these three types of elements all have two methods. The parameters of querySelector and querySelectorAll must be strings conforming to
css selector
. The difference is that querySelector returns an object, and querySelectorAll returns a collection (NodeList).
Currently, IE8/9 and the latest versions of Firefox/Chrome/Safari/Opera already support them. If you want to get the elements whose class attribute is "red" on the page, in addition to using document.getElementsByClassName('red'), you can also use document.querySelector('.red') and document.querySelectorAll('.red' ).
But there are errors in the implementation of Element.querySelector and Element.querySelectorAll, as follows
[code]
< ;a href="http://www.sina.com.cn">SINA
In browsers that support these two methods, you can see that "http://www.sina.com.cn" pops up respectively. /", and "1". It means that the desired element or element collection has been queried. This is inconsistent with the W3C definition. According to the definition, "div a" should be searched within the range of element d1, and there is no div in d1 at all. Therefore null, empty collections should be returned respectively.
In jQuery 1.4.2 and previous versions, only document.querySelectorAll is used, and Element.querySelectorAll is not used. Element.querySelectorAll was used after jQuery 1.4.3, but it was fixed. Add "#__sizzle__" before the selector to force it to search within the specified element (lines 3903-3918). Simplified