javascript - why is first child of the body undefined? -
it runs ok on body outputs undefined on first child. doing wrong?
function doit(e) { console.log('tag: ' + e.tagname); console.log('nt: ' + e.nodetype); (var childelement in e.children) { doit(childelement); } } doit(document.body);
<body> <ul id="fork"> <li id="myli" class="myclass">hello</li> </ul> </body>
edit:
this helped me finish css cleaner. check out [jsfiddle]
for...in
loops iterate enumerable properties, want iterate values. then,
for (var prop in e.children) doit(e.children[prop]);
however, that's bad way of iterating array-like objects. better use
for (var = 0; i<e.children.length; ++i) doit(e.children[i]);
or es5 array methods
[].foreach.call(e.children, doit);
function doit(e) { console.log('tag: ' + e.tagname); console.log('nt: ' + e.nodetype); [].foreach.call(e.children, doit); } doit(document.body);
<ul id="fork"> <li id="myli" class="myclass">hello</li> </ul>
Comments
Post a Comment