The getall method is private in the manipulation module. Code There are only a few simple lines, as shown below
Copy 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 [];
}
}
From the function name, we can see that this method is used to obtain all the child elements of the HTML element. Three Internal branches
1. First, determine whether ELEM has the getelementsbytagname method. If you use the getelementsbytagname method to retrieve all child elements, return.
2. getelementsbytagname is not supported, and then ELEM determines whether the queryselectorall method is supported. For example, the queryselectorall method is supported to obtain element sub-elements and return them.
3. getelementsbytagname and queryselectorall are not supported. An empty array is returned.
I was confused when I looked at this code. I felt that the second branch was a bit redundant.
1. getelementsbytagname is the API in Dom Level 2 (earlier). Currently, all browsers should have supported it. Since it is supported, it will not go to the second branch and return directly. Isn't the subsequent code redundant.
2. queryselectorall is an API (newer) in Dom Level 3, which is not supported by IE6/7.
Do you also think that the next two branches are redundant? Or can we find out why there are no additional reasons? You only need to find the element ELEM that meets the following conditions.
"ELEM does not have the getelementsbytagname method, but there is a queryselectorall method"
After seeking from multiple parties, the discussion finally found the answer (found by the Mavericks ). Documentfragment meets this condition.
Copy code The Code is as follows: var frag = Document. createdocumentfragment ();
Alert ('getelementsbytagname' in frag );
Alert ('queryselectorall' in frag );
In ie9/Chrome/Safari/Firefox/Opera, the preceding Code displays false and true.
This is not an explanation.
NOTE: Special Points of the documentfragment object
1. The createelement method is available in IE6/7/8 and does not exist in other browsers (ie9/10/Safari/Chrome/Firefox/opera ).
2. The getelementsbytagname method is not available in ie9/10/Firefox/Safari/Chrome/opera, but the queryselectorall method is available.
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