JavaScript Dom_ Gets the element method _getelementsbytagname () Gets the node list of the same element

Source: Internet
Author: User
Tags tag name tagname

Whether it is getElementById or getElementsByTagName, when passing parameters, not all browsers must be case-sensitive, in order to prevent unnecessary errors and trouble, we must insist on a case-sensitive habit.

First, get the node by tag name

1, getElementsByTagName () method gets the node by the tag name, because the label name repeats, so it returns an array of objects htmlcollection (NodeList), which holds a list of all nodes of the same element name.

<script type= "Text/javascript" >window.onload=function () {                                 if(document.getElementsByTagName) {//determine if getElementsByTagName is supported            varLi = document.getElementsByTagName (' li ');//parameter passed in a tag name, passing in * means getting all the elementsAlert (LI);//returns a collection of arrays htmlcollectionalert (li.length);//returns the number of arraysAlert (li[0]);//htmlhtmlelement Node object, here is the first Li tagAlert (Li.item (0));//Ibid .                        //Gets the node object so that it can get the content through the node object, etc. propertiesAlert (li[0].tagname); Alert (li[0].innerhtml); }Else{alert ("Browser not compatible, please replace")        }    };</script>

2, through the wildcard character to get the HTML tag element, IE browser when using wildcards, the document will be the beginning of the HTML specification declaration as the first element node. Firefox's Firebug will count as one, and it's not okay to turn it off.

<script type= "Text/javascript" >window.onload=function () {                                 if(document.getElementsByTagName) {//determine if getElementsByTagName is supported            varLi = document.getElementsByTagName (' * ');//parameter passed in a tag name, passing in * means getting all the elementsAlert (LI);//returns a collection of arrays htmlcollectionalert (li.length);//returns the number of arraysAlert (li[0]);//returns the first Htmlhtmlelement node objectAlert (Li.item (0));//Ibid .alert (li[0].tagname);//The first element in IE browser is the declaration of a document!}Else{alert ("Browser not compatible, please replace")        }    };</script>

    

3, get the Body node object, because there can be only one body node object in an HTML document, so you can get:

function () {                                 if(document.getelementsbytagname) {                // Determines whether getElementsByTagName is supported             var body = document.getelementsbytagname (' body ') [0];                 // if no addition [0] returns an array, plus [0] returns the Body node object             alert (body);                                                } Else {            alert ("Browser is incompatible, please replace")        }    };

4, you can also first get the ID through getElementById, and then through Getelementbytagname get this ID under the specified element, rather than get the whole document of that element

   

JavaScript Dom_ Gets the element method _getelementsbytagname () Gets the node list of the same element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.