javascript 浏览器兼容性心得

来源:互联网 发布:域名流量劫持 编辑:程序博客网 时间:2024/04/26 10:47

javascript 浏览器兼容性心得(IE、Fixfox、Safari、Opera内核的浏览器)

感谢cindysaj的分享http://cindysaj.iteye.com/blog/696723

1、脚本中px 一定要加上,否则Fixfox、Safari、Opera可能有问题

2、获取对象 使用getElementById 均支持 尽量不要使用document.all或document.form等集合方法

3、window.event

(1)  通用写法

<input type=”button” name=”someButton” value=”提交” onclick=”javascript:gotoSubmit(event)”/> 
… 
<script language=”javascript”> 
function gotoSubmit(evt) { 
evt = evt ? evt : (window.event ? window.event : null); 
… 
alert(evt);              // use evt 
… 

</script>

(2)event.clientX和event.clientY属性 通用

(3)even对象 通用var el=evt.srcElement || evt.target;

(4)keyCode问题,正确写法:

    var UA=navigator.userAgent;
    if (/msie/i.test(UA) || /opera/i.test(UA)){
        return String.fromCharCode(evt.keyCode);
    }else{
        return String.fromCharCode(evt.charCode);
    }

4、事件function通用写法 如:

  document.onclick=function(event) { 
    var evt = null;
    if (arguments.length==1){
        evt=arguments[0];
    }else{
     evt = (window.event ? window.event : null);
    }

    alert(evt); 
}

5、标签的x和y的坐标位置 通用:object.offsetLeft 和 object.offsetTop

6、窗体的高度和宽度 document.body.clientWidth和document.body.clientHeight

7、添加事件 
IE:element.attachEvent(”onclick”, func);。 
FF:element.addEventListener(”click”, func, true)。 
通用:element.onclick=func。虽然都可以使用onclick事件,但是onclick和上面两种方法的效果是不一样的,onclick只有执行一个过程,而attachEvent和addEventListener执行的是一个过程列表,也就是多个过程。例如:element.attachEvent(”onclick”, func1);element.attachEvent(”onclick”, func2)这样func1和func2都会被执行。

8、父节点、子节点 通用parentNode、parentNode.childNodes

标签的自定义属性

9、关于frame 需要写上id和name

获取frame对象,通用写法:window.frames["idORname"]

但是要调整frame属性,最好采用getElementById("id")

10、showModalDialog

都支持,但是参数通用写法如center:1; 如:

Opera不能弹出窗体,Safria不能居中,需要自己定义left和top,

通用写法:

function openDialog(url, width, height)
{
    var top = (window.screen.availHeight-height) / 2;
    var left = (window.screen.availWidth-width) / 2;
    var UA=navigator.userAgent;
    if (/gecko/i.test(UA) || /opera/i.test(UA)){
        window.open(url, "", "height="+height+"px,width="+width+"px,menubar=0,toolbar=0,status=1,resizable=1,scrollbars=1,top="+top+"px,left="+left+"px");
    }else{
       return window.showModalDialog(url, "", "dialogWidth:" + width + "px;" + "dialogHeight:" + height + "px;resizable:1;status:0;menubar:0;scrollbars:1;titlebar:0;toolbar:0;center:1;help:0;left:"+left+"px;top:"+top+"px;");
}

 

不要使用showModelessDialog(仅IE支持)
}

11、innerHTML(innerText FF不支持)

12、自定义属性
IE:如果给标签div1定义了一个属性value,可以div1.value和div1[”value”]取得该值。 
FF:不能用div1.value和div1[”value”]取。 
通用:div1.getAttribute(”value”)。


转自:http://blog.csdn.net/jsyhello/article/details/6315104

原创粉丝点击