4 ms·
element.querySelector and .querySelectorAll [1] provide this functionality. Sizzle [2], the selector engine at the core of jQuery, uses it where available and w
by davecardwell 14y ago
element.querySelector and .querySelectorAll [1] provide this functionality. Sizzle [2], the selector engine at the core of jQuery, uses it where available and works around some browser inconsistencies.
[1] http://caniuse.com/queryselector http://caniuse.com/queryselector
[2] http://sizzlejs.com/ http://sizzlejs.com/
- masklinn 14y ago> element.querySelector and .querySelectorAll [1] They provide a limited part of the selection function. Altering nodes, creating new nodes (and adding them to the DOM tree) and programmatic DOM tree traversals? Not so much, you get very clumsy and verbose APIs for that in the regular DOM. Same with DOM event handling, jQuery provides nice shortcuts for binding and delegation, the DOM... not so much (even if you limit yourself to IE9 and don't have to handle the garbage that is the oldIE event model) Also, as usual with the fucking DOM, querySelectorAll returns a NodeList which means you can't trivially use higher-order iterators on it (you've got to use the "generics" version and hope it works correctly everywhere). Fuck, that makes me angry again, the DOM is such a clusterfuck of an API.
- jackmoore 14y agoIterating a NodeList isn't so bad: Array.prototype.forEach.call( document.querySelectorAll('a'), function(el){ console.log(el); }); But, I agree with the point. If the NodeList isn't live, like it is for the other DOM querying methods, then what is the point in returning a NodeList instead of an array?
- masklinn 14y agoThe DOM WebIDL does not know what an array is. Though an IDL extension/annotation added some time ago allows inheriting IDL interfaces from core language objects or interfaces rather than the root object (http://dev.w3.org/2006/webapi/WebIDL/#ArrayClass http://dev.w3.org/2006/webapi/WebIDL/#ArrayClass), and DOM4 adds this annotation to NodeList http://dom.spec.whatwg.org/#interface-nodelist http://dom.spec.whatwg.org/#interface-nodelist
- ahoge 14y agoFWIW, they fixed that crap in Dart. All those array-like things are indeed just regular arrays.