html - How to identify tags in a node list in Javascript? -
when html looks this:
<div id="aa1"><h1>aaaa</h1><h2 class="center">bbbb</h2></div>
and javascript looks this:
var list= document.getelementbyid("aa1").childnodes;
i node list looks this:
list= nodelist[h1,h2.center]
but, if html looks this:
<div id="aa1"><h3>cccc</h3><h1>aaaa</h1><h2 class="center">bbbb</h2></div>
i node list looks this:
list= nodelist[h3,h1,h2.center]
so, this:
if(list[0]==="<h3>"){console.log("yes, list[0] = <h3>");}
but doesn't work.
how can identities of each tag in node list?
use .nodename
property.
if(list[0].nodename==="h3"){console.log("yes, list[0] = <h3>");}
there rare edge cases name returned lowercase, safety, add .touppercase()
after .nodename
.
also, though don't have text nodes contend with, if did, use .children
instead of .childnodes
.
Comments
Post a Comment