How to convert the data returned by ChildNodes to a dimension array

Source: Internet
Author: User
Convert the data returned by ChildNodes to an array of methods            function Converttoarray (nodes) {                var array=null;                try{                    Array=array.prototype.slice.call (nodes,0);                } catch (ex) {                    array=new array ();                    for (Var i=0,len=nodes.length;i<len;i++) {                        array.push (nodes[i]);                    }                }                return array;            }            Methods for filtering nodes            function getelementlist (arr,value) {                var arrlist=new Array ();                for (Var i=0,len=arr.length;i<len;i++) {                    if (arr[i].nodetype==value) {                        arrlist.push (arr[i]);                    }                }                return arrlist;            }

Each node has a ChildNodes property, which holds the NodeList object. NodeList is a class array object that holds an ordered set of nodes that can be accessed using a location. However, it is important to note that although we can access the object through square brackets, and nodelist has the length property, it is not an instance of the array.

Of course, we can also access the nodes in NodeList through the item () method, just as you would with square brackets. Both of these are not problematic, and because of the simplicity of the square brackets, they are more favored by developers.

In this, I still recommend the above nodelist to the method of the array, but because in IE8 and below is not supported Array.prototypr.slice.call (sonenode.childnodes,0); So we can only convert the dimension array by enumerating the nodelist. Try-catch catches the error and creates the array manually.

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.