The getAll method is private and in the manipulation module. The code only has a few simple lines, as follows
function getAll( elem ) {
if ( typeof elem.getElementsByTagName !== "undefined" ) {
return elem.getElementsByTagName( "*" );
} else if ( typeof elem.querySelectorAll !== "undefined" ) {
return elem.querySelectorAll( "*" );
} else {
return [];
}
}
You can know this method from the function name Used to get all child elements of the passed in HTML element. There are three internal branches
1. First determine whether elem has a getElementsByTagName method. If so, use the getElementsByTagName method to obtain all child elements and return.
2. GetElementsByTagName is not supported. Then determine whether elem supports the querySelectorAll method. If it supports using the querySelectorAll method to obtain the element's sub-elements, it will return.
3, getElementsByTagName and querySelectorAll are not supported and return an empty array.
When I looked at this code, I was confused and felt that the second branch was a bit redundant.
1, getElementsByTagName is an API in
DOM Level 2 (earlier). All current browsers should already support it. Since they are all supported, there will be no need to enter the second one. branched and returned directly. Isn't the following code redundant?
2. querySelectorAll is an API in
DOM Level 3 (newer) and is not supported by IE6/7.
After seeing this, do you think the last two branches are redundant? Or can you find a reason why it is not redundant? That is, just find the element elem that meets the following conditions.
"elem does not have a getElementsByTagName method, but it has a querySelectorAll method"
After many searches, the answer was finally found through discussion (discovered by Xiaoniu). DocumentFragment meets this condition.
var frag = document.createDocumentFragment();
alert('getElementsByTagName' in frag);
alert('querySelectorAll' in frag);
The above code pops up false in IE9/Chrome/Safari/Firefox/Opera. , true.
No more explanation at this point.
Note: Several special points of the DocumentFragment object
1. IE6/7/8 has the createElement method, but other browsers (IE9/10/Safari/Chrome/Firefox/Opera) do not
2. There is no getElementsByTagName method in IE9/10/Firefox/Safari/Chrome/Opera, but there is a querySelectorAll method.
Related:
http://www.jb51.net/article/30352.htmhttps://developer.mozilla.org/en/DOM/document .createDocumentFragmenthttps://developer.mozilla.org/En/DOM/DocumentFragmenthttp://www.w3.org/TR/DOM-Level -3-Core/core.html#ID-B63ED1A3