IE6, IE7 setattribute do not support Class/for/rowspan/colspan and other attributes _javascript skills

Source: Internet
Author: User
such as setting the Class property
Copy Code code as follows:
El.setattribute (' class ', ' abc ');

The style "ABC" in IE6/7 will not work, although using El.getattribute (' class ') can fetch the value "ABC".

Also as for property
Copy Code code 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 lab sets the for attribute, clicking on the label automatically selects the corresponding checkbox. But the above settings in IE6/7 click will not check checkbox.

A similar situation also occurs on the cellspacing/cellpadding. The summary is as follows:
Class
For
CellSpacing
cellpadding
TabIndex
ReadOnly
MaxLength
RowSpan
colspan
Usemap
Frameborder
Contenteditable
Therefore, it is necessary to consider the specificity of the above attributes in IE6/7 when writing an interface method for setting element properties across browsers. As follows
Copy Code code 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 property name directly, and secondly, IE6/7 attributes that are not listed above still use the original property name, and finally these special attributes (with the same name as the JS keyword, such as for,class) use Fixattr.
OK, now don't consider classname/htmlfor, all use class/for can.
Copy Code code as follows:

Dom.setattr (el, ' class ', ' Red ');
Dom.getattr (el, ' class ');
Dom.setattr (el, ' for ', ' userName ');
Dom.getattr (el, ' for ');

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.