js在兼容IE和FireFox方面的一些方法

1、创建一个Element,通用的写法为createElement(“div”)
   IE中也可以这样写createElement(“

“),但Firefox不认
2、IE中的width、height与Firefox中概念不同,IE中width=FireFox中的width+2*borderWidth+2*Padding
3、动态添加css代码
   IE:
cssStr = “p { color:#FF0000;} a { font-size:9pt;}”;
var style = win.document.createStyleSheet();
style.cssText = cssStr;

FireFox:
cssStr = “p { color:#FF0000;} a { font-size:9pt;}”;
var style = win.document.createElement(“style”);
style.type = “text/css”;
style.innerHTML = cssStr;
win.document.getElementsByTagName(“HEAD”).item(0).appendChild(style);

4、table在后面添加加行或列,通用写法insertRow(-1),insertCell(-1)
   IE中insertRow(),insertCell()这样写也可以,Firefox不认
5、警告对话框alert(),IE中书写时无参数则默认参数为空字符串,Firefox中则必须输入参数,传空参数则要写成alert(“”);
6、给element.style.width赋值必须写成24px,只写数字24的话,FireFox会不认,IE里都可以
7、在使用Element,并给其绑定了onclick、onmouseover、onmousedown、onmouseout等事件
    element.onclick = function() { alert(“hello kitty”); };
   时,需注意
   将此元素添加到上级元素上时要用appendChild,不可以在上级中使用innerHTML操作,这样会使事件无效

You May Also Like

About the Author: 邢磊

发表评论

您的电子邮箱地址不会被公开。