JavaScript dom_ Get/Set/remove properties of a specific element node _getattribute ()/setattribute ()/removeattribute ()

Source: Internet
Author: User

One, gets the value of the property of a specific element node _getattribute ()

1, the GetAttribute () method gets the value of an attribute in the element. It is somewhat different from the method used to get property values directly from the.

<script type= "Text/javascript" >window.onload=function(){         varbox = document.getElementById (' box ');                        alert (BOX.BBB); //Gets the custom attribute value of the element, non IE does not support custom properties No, the result is undefinedAlert (Box.getattribute (' BBB '));//get custom property values for an element this method can be compatible with custom propertiesAlert (Box.getattribute (' class '));//The getattribute method gets the value of the class attribute in the tag when using classname in ie6,7, and other uses of class need to be compatible                        //get the value of the class property across browsers            if(Box.getattribute (' className ') = =NULL) {alert (Box.getattribute (' Class ')); }Else{Box.getattribute (' ClassName '); }    };</script>

Second, set the value of the attribute of the specific element node _setattribute ()

1, the SetAttribute () method sets an attribute and value in the element. It needs to accept two parameters: property name and value. If the property itself already exists, it will be overwritten.

<script type= "Text/javascript" >     function  () {                                     var box = document.getElementById (' box ');            Box.setattribute (' title ', ' I am the title ');            alert (box.title);       }; </script>

2, the use of this method to set the class and style property is not valid in ie6,7 (so not recommended)

<script type= "Text/javascript" >    function() {         var box = document.getElementById (' box ');         Box.setattribute (' style ', ' color:green '); // ie6,7 Invalid         Box.setattribute (' class ', ' pox '); // ie6,7 Invalid               }; </script>

Three, removeattribute () can remove attributes from an HTML element. IE6 not supported

<script type= "Text/javascript" >     function  () {                                     var box = document.getElementById (' box ');            Box.removeattribute (' title ');            alert (box.title);    }; </script>

JavaScript dom_ Get/Set/remove properties of a specific element node _getattribute ()/setattribute ()/removeattribute ()

Related Article

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.