当我们点击BACKSPACE / DEL / do CUT时,IE9中的oninput不会触发 [英] oninput in IE9 doesn't fire when we hit BACKSPACE / DEL / do CUT

查看:171
本文介绍了当我们点击BACKSPACE / DEL / do CUT时,IE9中的oninput不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们点击BACKSPACE / DEL / do CUT时,我们使用什么最干净的解决方案来解决IE9不会触发输入事件的问题?

What's the cleanest solution we use to overcome the problem that IE9 doesn't fire the input event when we hit BACKSPACE / DEL / do CUT?

最干净的我意味着代码发臭没有。

By cleanest I mean code stinks not.

推荐答案

我开发了一个 IE9 polyfill 用于退格/删除/剪切。

I developed an IE9 polyfill for the backspace/delete/cut.

(function (d) {
  if (navigator.userAgent.indexOf('MSIE 9') === -1) return;

  d.addEventListener('selectionchange', function() {
    var el = d.activeElement;

    if (el.tagName === 'TEXTAREA' || (el.tagName === 'INPUT' && el.type === 'text')) {
      var ev = d.createEvent('CustomEvent');
      ev.initCustomEvent('input', true, true, {});
      el.dispatchEvent(ev);
    }
  });
})(document);

出于某种原因,在IE9中,当您在文本字段内删除时, selectionchange 触发事件。

For some reason, in IE9, when you delete inside a textfield, the selectionchange event is triggered.

因此,您唯一需要做的是:检查选择的活动元素是否为输入,然后我告诉元素发送输入事件,就像它应该做的那样。

So, the only thing you need to do is: check if the active element of the selection is an input, and then I tell the element to dispatch the input event, as it should do.

将代码放在任何地方您的页面,所有删除操作现在将触发输入事件!

Put the code anywhere in your page, and all the deletion actions will now trigger the input event!

这篇关于当我们点击BACKSPACE / DEL / do CUT时,IE9中的oninput不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆