Chrome中的dispatchEvent但在IE11中不是 [英] dispatchEvent in Chrome but not in IE11

查看:194
本文介绍了Chrome中的dispatchEvent但在IE11中不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码提交表单:

I am using the following code to submit to a form:

element.dispatchEvent(new Event("submit"));

检查器返回错误:

对象不支持此操作

Object doesn't support this action

这在Chrome浏览器中有效.

This works in Chrome.

此命令的目的是在单击时在表格上进行分区调用Submit事件.

The purpose of this command is to make a division call the submit event on a form when clicked.

不能使用jQuery

推荐答案

这是考虑到将来的更改,使其在IE11和其他浏览器上正常工作的最佳方法.

This is the best way to make it work for IE11 and other browsers with considering future changes.

var event;
if(typeof(Event) === 'function') {
    event = new Event('submit');
}else{
    event = document.createEvent('Event');
    event.initEvent('submit', true, true);
}

$el.dispatchEvent(event);

这篇关于Chrome中的dispatchEvent但在IE11中不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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