document.getElementsByTagName

 document.getElementsByTagName can obtain a collection of objects with a specific TagName tag name in an HTML file. Its usage is similar to getElementsByName and getElementById , but the specific tag obtained by getElementsByTagName is not a single one. If there are many tags with the same Tag Name in the webpage, it will be retrieved once. , And sorted according to the position of each Tag Name. Almost all mainstream browsers support the document.getElementsByTagName property.


Document.getElementsByTagName basic syntax

var elements = document.getElementsByTagName( tag name );


Please put the specific tag name (tag name) you are looking for directly into the function.

document.getElementsByTagName example
<script type="text/javascript">
function GoCountNum(x){
 alert(document.getElementsByTagName(x).length); }
</script>
<form></form><br>
<form></form><br>
<table></table><br>
<input type="button" onclick="GoCountNum('form')" value="計算表單數量">
<input type="button" onclick="GoCountNum('table')" value="計算表格數量">
In the example, we first prepared two HTML Form tags and one HTML Table tag, and wrote a simple function to calculate the number. The two function buttons are used to calculate the number of forms and the number of forms. The button will call the GoCountNum function. And count the total number of HTML Form or HTML Table .

From this example, it can be seen that getElementsByTagName is very useful for specific tags in such a large number of statistical HTML files. This part is similar to getElementsByName . If you want to obtain the value of a specific id separately, it is easier to use getElementById .

Post a Comment

0 Comments