js兼容

//document.getElementsByClassName();

if (!document.getElementsByClassName) {
        document.getElementsByClassName = function (cls) {
            var ret = [];
            var els = document.getElementsByTagName('*');
            for (var i = 0, len = els.length; i < len; i++) {

                if (els[i].className.indexOf(cls + ' ') >=0 || els[i].className.indexOf(' ' + cls + ' ') >=0 || els[i].className.indexOf(' ' + cls) >=0) {
                    ret.push(els[i]);
                }
            }
            return ret;
        }
    }

//window.event   
IE:有window.event对象   
FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event) 

//鼠标当前坐标   
IE:event.x和event.y。   
FF:event.pageX和event.pageY。   
通用:两者都有event.clientX和event.clientY属性。

//标签的x和y的坐标位置:style.posLeft 和 style.posTop   
IE:有。   
FF:没有。   
通用:object.offsetLeft 和 object.offsetTop。  

//窗体的高度和宽度   
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此时页面一定要有body标签。   
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。   
通用:document.body.clientWidth和document.body.clientHeight。

//添加事件   
IE:element.attachEvent("onclick", function);。   
FF:element.addEventListener("click", function, true)。

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

//window.location.href问题   
说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location  
解决方法:使用window.location来代替window.location.href 

   

原文地址:https://www.cnblogs.com/change-oneself/p/5000943.html