浏览器差异

本文总结了在前端开发过程中遇到的IE7、8与其他浏览器之间的兼容性问题,包括bind函数的支持情况、keydown事件的使用差异、childNodes属性的问题等,并提供了相应的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端做了一段时间了,比较蛋疼的事情是IE7、8与其他的浏览器的差异,记下这些备忘。(是不是想说我闲的蛋疼要去维护IE7、8干嘛。。。你去问qa啊摔!

(1)bind函数:

Function.protype.bind这个函数还是蛮有用的,大部分浏览器都支持,nodejs也支持,除了万恶的IE。根据微软的官方文档,这玩意在以下浏览器模式中不支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式。但是不用有时会蛋疼,好在Mozilla那边有兼容性代码提供,毕竟这玩意好像最先是Firefox支持的,代码如下

if (!Function.prototype.bind) {
  Function.prototype.bind = function(oThis) {
    if (typeof this !== 'function') {
      // closest thing possible to the ECMAScript 5
      // internal IsCallable function
      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
    }

    var aArgs   = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP    = function() {},
        fBound  = function() {
          return fToBind.apply(this instanceof fNOP
                 ? this
                 : oThis,
                 aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}
代码的实现原理无非就是利用了protype来添加函数。

(2)document.keydown事件

经测试,IE8环境下,document.keydown=function(e){}这种场景下e是undefined,需要使用window.event;

(3)IE7不支持inherit

(4)childNodes属性还是别用了。。。比较坑爹。。。用children属性吧

(5)div的blur事件在元素没有tabindex属性的情况下无法激发

(6)Firefox不支持innerText属性,需要改用textContent,懒得改的换就用jQuery吧

(7)chrome下,似乎有tabindex属性的div,其内部的div的click事件无法激发,经测试,给内部div添加tabindex,改用focus事件可解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值