At first, a lot of people would take the jquery selector to compare these two APIs (me too), and the similarities and differences would have been fine, but some students had misunderstood the implementation of the two APIs in the browser, especially when DOM element raised the API.
Here's a sample of my jsfiddle, and I'll start by saying:
Copy Code code as follows:
(function (Global) {
Global.doc = document;
Global.body = Doc.getelementsbytagname (' body ') [0];
global.$ = function (ID) {
return Doc.getelementbyid (ID);
}
Global. Logger = function (ID) {
This.logelem = $ (ID);
This.logarr = [];
};
Global. Logger.prototype = {
Constructor:global.logger,
Append:function (comment) {
This.logArr.push (' <p> ' + comment + ' </p> ');
},
Flush:function () {
This.logElem.innerHTML = This.logArr.join (");
},
Clear:function () {
This.logElem.innerHTML = ';
This.logarr = [];
}
};
}) (this);
(function () {
var logger = new Logger (' log ');
var items = $ (' inner '). Queryselectorall (' #main h4.inside ');
Logger.append (items.length);
for (var i = 0, len = items.length i < len; i++) {
Logger.append (items[i].innerhtml);
}
Logger.flush ();
})();
The misconception is that the realization of the $ (' inner '). Queryselectorall (' #main h4.inside ') is understood, and many people begin to think of it directly from the children of Div[id= ' inner ' (Me too), this # Main is a bit of an eyesore. It actually looks up from the entire document according to the selector string, and then returns the child nodes belonging to div[id= ' inner '. Many people will wonder, then why not follow the parent node to find the child node directly to achieve it? Like Elem.getelementsbytagname, my idea is to be flexible selector string.
Queryselector returns only the first element of the match, or null if there is no match.
Queryselectorall returns a matching set of elements that, if there is no match, returns an empty nodelist (an array of nodes).
And the return result is static, and subsequent changes to the document structure do not affect the results obtained before.
Currently Ie8+,ff,chrome supports this API (selector string in IE8 only supports css2.1).