关于JS、jQuery中个别方法的浏览器兼容性问题之总结(陆续更新)

一、CSS样式的获取

比如,我想获取当前页面中font-size样式的值

在IE浏览器中可以使用 元素.currentStyle.具体属性 的方法获得具体的值。

但是在360和FF中currentStyle则会失效,这个时候要用到getComputedStyle(元素, 伪类名).属性 的方法。其中伪类名如果不存在则写null。
兼容性写法如下:

var target = document.getElementByID("a");
var tag_fontsize  = null;
if(target.currentStyle)//IE
{
    tag_fontsize = target.currentStyle.fontSize;
}
else//360 and FireFox
{
    tag_fontsize = getComputedStyle(target, null).fontSize;
}

 二、获取浏览器的尺寸

/**获取屏幕的宽度*/
var winWidth = 0;//屏幕的宽px
if(typeof(window.innerWidth) == "number")//360和火狐浏览器
{
    winWidth = window.innerWidth;
}
else if ((document.body) && (document.body.clientWidth))//IE浏览器
{
    winWidth = document.body.clientWidth;
}

 三、触发window.onresize函数

window.onresize函数只在IE浏览器中有效,在360 和FF中都无效,所以,最明智的解决方法就是改用jQuery:$(window).resize(function(){blabal……}); 代替 JS中的window.onresize = fucntion(blabla……);

 

你可能感兴趣的:(jquery)