01月16, 2018

IE9下的onChange事件bug

问题表现

在IE9浏览器上,在输入框输入数字时报错“找不到成员”

代码

<input  onChange={this.onInputChange}
         value={this.state.callText}
         placeholder={MEETINGI18N["invite.call.input.placeholder"]}
/>
onInputChange: function (e) {
      e.stopPropagation();
      var value = $(e.target).val();
        this.setState({
            callText: value
        });
    },

分析过程

在IE下调试,发现在执行到stopPropagation 时报错。进入方法内部发现:

stopPropagation: function () {
    var event = this.nativeEvent;
    if ("development" !== 'production') {
      "development" !== 'production' ? warning(event, 'This synthetic event is reused for performance reasons. If you\'re ' + 'seeing this, you\'re calling `stopPropagation` on a ' + 'released/nullified synthetic event. This is a no-op. See ' + 'https://fb.me/react-event-pooling for more information.') : undefined;
    }
    if (!event) {
      return;
    }

    if (event.stopPropagation) {
      event.stopPropagation();
    } else {
      event.cancelBubble = true;
    }
    this.isPropagationStopped = emptyFunction.thatReturnsTrue;
  },

在IE8下,不支持stopPropagation方法,所以执行了event.cancelBubble = true; 没有问题。 在IE9下,nativeEvent既没有stopPropagation 方法,也没有cancelBubble成员,所以报错“找不到成员”。

为什么IE9不支持stopPropagation

https://connect.microsoft.com/IE/feedback/details/1008251/onchange-event-not-firing-in-ie9 初步看起来是兼容性问题或者ie9对stopPropagation的部分支持。待进一步看文档查看原因。

待讨论

  • input 的onchange事件是否支持stopPropagation
  • input的onchange事件收有必要stopPropagation, 如果不阻止冒泡,会触发它的上级组织的事件吗?触发什么事件?
  • IE9在哪些情况下是支持stopPropagation的?
  • 高级浏览器对input onchange事件里面的stopPropagation的实现是怎样的?

待学习

https://reactjs.org/docs/events.html#event-pooling

本文链接:http://fengbaiyang.cn/post/ie9-onchange-bug.html

-- EOF --

Comments

暂不支持评论,如有问题,请发邮件至baiyang.feng@outlook.com。 望不吝赐教~