JS's setattribute and GetAttribute

Source: Internet
Author: User

1.ele.getattribute (AttributeName);

Returns the specified property value of the element, or null if the element does not have the attribute

2.ele.setattribute (Attributename,value);

Sets the value of the attribute for the element and, if there is no attribute, creates the property and assigns a value

3. The settings for the properties in IE 7 and earlier versions should use a different name, in order to be compatible with IE

<Script>Dom=(function(){           varfixattr={tabindex:'TabIndex', ReadOnly:'readOnly',                  ' for':'htmlfor',                  'class':'ClassName', MaxLength:'MaxLength', cellspacing:'cellspacing', cellpadding:'cellpadding', rowspan:'RowSpan', colspan:'ColSpan', Usemap:'Usemap', frameborder:'frameborder', contenteditable:'contenteditable'           },                   //analog settings attribute,Div=Document.createelement ('Div'); Div.setattribute ('class','T'); varsupportsetattr=Div.classname=== 'T'; return{setAttr:function(el, name, Val) {El.setattribute (supportsetattr?name: (Fixattr[name]||name), Val); }, GetAttr:function(el, name) {returnEl.getattribute (supportsetattr?name: (Fixattr[name]||name));                }              }        })(); Window.onload=function(){               varmydiv=document.getElementById ("D1"); Dom.setattr (Mydiv,'class', 'BG'); }    </Script>

In IE 7 and earlier, SetAttribute (' class ', ' fo ') can add class= ' fo ' to the element.

GetAttribute (' class '), can return fo value, but the class added for the element does not work, should be set for IE 7 and earlier classes with ClassName, instead of class

4.

GetAttribute (AttributeName), not only can get the default attribute value of the element, but also can get the custom attribute value ele.attributename or ele[' attributename ' can only get the attribute value that the element default exists

5.

var node = Ele.getattributenode (attributename) Gets the attribute node Console.log (node);//name=value form Console.log (node.name); Console.log (Node.value);
<Body>    <PID= "P1"CustomData= "PMX">Ppp</P>    <Script>       varP=document.getElementById ("P1"); varPnode=P.getattributenode ("CustomData"); Console.log (Pnode)</Script></Body>

6.ownerElement returns the element attached to the attribute node

Grammar:

Attrobj.ownerelement

JS's setattribute and GetAttribute

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.