The puzzled analysis of GetAll method in JQuery 1.7.2 _jquery

Source: Internet
Author: User
The GetAll method is private, in the manipulation module. The code has only a few simple lines, as follows
Copy Code code as follows:

function GetAll (elem) {
if (typeof elem.getelementsbytagname!== "undefined") {
Return Elem.getelementsbytagname ("*");
else if (typeof elem.queryselectorall!== "undefined") {
Return Elem.queryselectorall ("*");
} else {
return [];
}
}

This method is used to get all the child elements of an incoming HTML element from the function name. There are three branches inside

1, first determine whether the Elem has a getElementsByTagName method, such as the use of the getElementsByTagName method to obtain all child elements and then return.
2, getElementsByTagName is not supported to determine whether Elem supports Queryselectorall methods, such as supporting the use of the Queryselectorall method to get element child elements back.
3,getelementsbytagname and Queryselectorall are not supported, return an empty array.

When looking at the code, some doubt, feeling that the second branch is a bit superfluous.

1,getelementsbytagname is the API in DOM Level 2 (earlier), all current browsers should have been supported, and since they are supported, they will not go into the second branch and return directly. The code behind is not all superfluous.
2,queryselectorall is the API (newer) in DOM level 3, IE6/7 not supported.

See here everyone is not also think that the back two branches is redundant? Or is it possible to find a reason that is not superfluous? That is, just find the element elem that meets the following criteria.
"Elem has no getElementsByTagName method, but there are queryselectorall methods."
After many search, the discussion finally found the answer (found by calf students). DocumentFragment satisfies this condition.

Copy Code code as follows:

var frag = document.createdocumentfragment ();
Alert (' getElementsByTagName ' in Frag);
Alert (' Queryselectorall ' in Frag);


The above code in the Ie9/chrome/safari/firefox/opera has popped up false,true.

To this, do not explain.

Note: Several special points of DocumentFragment objects
1,IE6/7/8 has createelement method, other browsers (Ie9/10/safari/chrome/firefox/opera) do not
There is no getElementsByTagName method in 2,ie9/10/firefox/safari/chrome/opera, but there are queryselectorall methods.
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.