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.