Question analysis of the getAll method in jQuery1.7.2 _ jquery

Source: Internet
Author: User
In jQuery1.7.2, The getAll method is private in the manipulation module. There are only a few simple lines of code, as shown below:

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.

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
Related Article

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.