event.preventDefault() is not working in IE 11 for custom events

烂漫一生 提交于 2019-12-02 02:38:56

I had the same problem and could solve it with the following hack:

var event = document.createEvent('CustomEvent');
event.initCustomEvent('custom', true, true, {});
event.preventDefault = function () {
    Object.defineProperty(this, "defaultPrevented", {get: function () {return true;}});
};
event.preventDefault();
event.defaultPrevented; // true

It looks like IE 11 does not set defaultPrevented for "synthetic" events created by code.

Example JSbin: http://jsbin.com/wohafoyo/1/edit

No matter what I tried in terms of event creation and dispatching, I could not make a synthetic click event's preventDefault set defaultPrevented to true.

Perhaps Polymer could modify CustomElement.prototype.preventDefault to set defaultPrevented.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!