Something very small thing that JavaScript can show – Search and count up Nodes in document –

この記事は約2分で読めます。

Search and count up Nodes in document.

Example, an HTML file shows it contains five sets of tags in the file.

That search and count up have done by the following a few JavaScript lines.

<!DOCTYPE html>
<head>
  <script>
     function countTags(n){ 
           var numtags = 0; 
           if (n.nodeType == 1 /*Node.ELEMENT_NODE*/){ 
                 numtags++;         
                 var children = n.childNodes; 
           } 
           for (var i=0; i < children.length; i++){ 
                 numtags += countTags(children[i]); 
           } 
           return numtags;
       }
  </script>
</head>
<body onload="alert('this document has' + countTags(document) + ' tags')">
This is a <i>sample</i> document.
</body>
</html>
タイトルとURLをコピーしました