About Htmlcollection and nodelist, record a bit.

Source: Internet
Author: User

Remember last year's work encountered old IE and modern browser JS about DOM operation compatibility issues, in Chrome, IE, Firefox test getelementsbytagname return value, the problem arises, Both Chrome and opera return the NodeList object, while IE and Firefox return the Htmlcollection object. At that time to check the information, a lot of articles said IE and Firefox did not follow the specifications, but the previous days I accidentally encountered a similar problem, browser (Chrome, IE, Firefox) a test, all returned to Htmlcollection, I and my little friends are stunned, Is there a problem with the spec or is chrome on the bandwagon? Turning over the new version of the book "JavaScript Advanced Programming", getElementsByTagName returns NodeList, but emphasizes that in an HTML document, a Htmlcollection object is returned. By the way also measured the next HTML5 added Getelementbyclassname, God Book said return is nodelist, in fact, browsers have returned htmlcollection. In fact, these two differences are not really small, they are dynamic, but because of this, each time access to nodelist and Htmlcollection will run a document-based query, so multiple access to these objects can affect performance. Do not write code like the following, resulting in an infinite loop:

var divs = document.getelementbytagname ("div"),     I,     div;  for (i = 0; i < divs.length; i++) {   = document.createelement ("div");   Document.body.appendChild (div);}

With a little modification, you can avoid infinite loops

var divs = document.getelementbytagname ("div"),     I,     Len,     div;  for (i = 0;len=divs.length; i < Len; i++) {     = document.createelement ("div");     Document.body.appendChild (div);}

Above is to set a Len variable and initialize its value, in the end is to cache the value of Divs.length, in writing JS code This small technology is very useful, this way to avoid infinite loops, but also to avoid repeated DOM traversal, both reduce the code overhead and improve performance.

Example of a jquery

// Bad  = $ (' #element '). Height (); $ (' #element '). CSS (' height ', h-20); // Recommended  = $ (' #element '= $element. Height (); $element. css (' height ', h-20);

The above is just a record, by the way, the debris of the front end of the trough, a variety of kernel browser, various terminals, various specifications of the endless modification and difficult to establish ... If there is a mistake welcome to point out and discuss

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.