In IE6 and IE7, setAttribute does not support attributes such as class/for/rowspan/colspan.

Source: Internet
Author: User

For example, set the class attribute.
Copy codeThe Code is as follows: el. setAttribute ('class', 'abc ');
In IE6/7, the style "abc" does not work, although the value "abc" can be obtained using el. getAttribute ('class ".

Another example is for attributes.
Copy codeThe Code is as follows:
<Label> name: </label> <input type = "checkbox" id = "name"/>
<Script>
Var lab = document. getElementsByTagName ('label') [0];
Lab. setAttribute ('for', 'name ');
</Script>

We know that when the for attribute is set for lab, clicking label will automatically select the corresponding checkbox. However, when you click IE6/7, the checkbox is not selected.

Similar cases occur on cellspacing/cellpadding. Summary:
Class
For
Cellspacing
Cellpadding
Tabindex
Readonly
Maxlength
Rowspan
Colspan
Usemap
Frameborder
Contenteditable
Therefore, when writing a common cross-browser interface method for setting element attributes, you must consider the particularity of the preceding attributes in IE6/7. As follows:
Copy codeThe Code is as follows:
Dom = (function (){
Var fixAttr = {
Tabindex: 'tabindex ',
Readonly: 'readonly ',
'For': 'htmlfor ',
'Class': 'classname ',
Maxlength: 'maxlength ',
Cellspacing: 'cellspacing ',
Cellpadding: 'cellpadding ',
Rowspan: 'rowspan ',
Colspan: 'colspan ',
Usemap: 'usemap ',
Frameborder: 'frameborder ',
Contenteditable: 'contenteditable'
},
Div = document. createElement ('div ');
Div. setAttribute ('class', 'T ');
Var supportSetAttr = div. className = 'T ';
Return {
SetAttr: function (el, name, val ){
El. setAttribute (supportSetAttr? Name: (fixAttr [name] | name), val );
},
GetAttr: function (el, name ){
Return el. getAttribute (supportSetAttr? Name: (fixAttr [name] | name ));
}
}
})();

First, the standard browser uses the original attribute name directly. Second, the attributes not listed above in IE6/7 still use the original attribute name. Finally, these special attributes (with the same name as the JS keyword, such as for and class) use fixAttr.
Now, you don't need to consider className/htmlFor. You can use class/.
Copy codeThe Code is as follows:
Dom. setAttr (el, 'class', 'red ');
Dom. getAttr (el, 'class ');
Dom. setAttr (el, 'for', 'username ');
Dom. getAttr (el, 'for ');

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.