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 ');