Home > Web Front-end > JS Tutorial > Analysis of doubts about the getAll method in jQuery 1.7.2_jquery

Analysis of doubts about the getAll method in jQuery 1.7.2_jquery

WBOY
Release: 2016-05-16 17:53:31
Original
1132 people have browsed it

The getAll method is private and in the manipulation module. The code only has a few simple lines, as follows

Copy the code The code is 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.

Copy code The code is as follows:

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.htm
https://developer.mozilla.org/en/DOM/document .createDocumentFragment
https://developer.mozilla.org/En/DOM/DocumentFragment
http://www.w3.org/TR/DOM-Level -3-Core/core.html#ID-B63ED1A3
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template