关于IE8的js兼容问题有很多,之前也遇到很多,不过没有记录,今天又处理两个,以后再遇到的时候我会在来完善,现在就先记录这两条。
1、Date.now() 这个方法通常用来获取当前时间的毫秒数,但是在IE8下不支持Date这个对象。但是我它有new Date(),所以我们在IE8下可以这样获取到当前时间毫秒数
var dte = new Date();
var now = dte.getTime();
2、document.getElementsByClassName()这个方法通过class的name获取对象,IE8也不支持该方法。后来我用jQuery的选择器来完成了它的功能,在IE8下是可以跑的,就是有点怪原生的js跑不掉,封装的居然可以,有时间一定去看看它的源码。
3、在IE8下对数组的indexOf方法不兼容,解决办法,在执行indexOf之前执行下面代码:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
4、那天在项目里遇到IE8下报console.log和console.error未定义,后来没办法写了如下的代码解决:
if(typeof window.console === "undefined"){
window.console = {
error: function(){},
log: function(){}
}
}
5、IE8透明问题
--body透明
IE8中body默认背景色为白色,可用background:transparent;来设置透明
--div透明
filter: progid:DXImageTransform.Microsoft.Gradient(startColorstr=#00000000,endColorstr=#00000000);
--iframe的背景透明
IE8中iframe的背景默认也是白色的,可添加allowTransparency="true" 属性来设置透明
目前就先记录这两条以后再慢慢完善,如果有看到我这篇博客的大侠有补充的可以留言,非常感谢!