gpt4 book ai didi

javascript - 如何让 fireEvent 方法在 IE8 中工作?

转载 作者:行者123 更新时间:2023-11-29 22:34:11 26 4
gpt4 key购买 nike

我正在尝试使用 IE 的 fireEvent 方法提交表单。下面是一些简单的测试代码:

<html>
<head>
<script type="text/javascript">
function fireSubmit () {
if (document.createEventObject) {
var event = document.createEventObject ();
var fired = document.forms['fireForm'].fireEvent("onsubmit", event);
alert("event fired: " + fired + "; event returnValue: " + event.returnValue);
}
}
</script>
</head>
<body>
<button onmouseover="fireSubmit();">
Hover to submit
</button>

<form name="fireForm" action="action.html" method="post">
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

下面是应该提交的简单 action.html:

<html>
<body>
<script>alert('submitted');</script>
</body>
</html>

如果我将鼠标悬停在按钮上,事件会创建并且 IE 声称它已被触发。弹出一个警告,上面写着“事件触发:真;事件返回值:未定义”,但 action.html 警告从未显示,returnValue 也从未设置为与this 相悖的内容。 claim 。但是,如果我只是单击提交按钮,实际上提交了表单并显示了“已提交”对话框。

我做错了什么?我是否以某种令人发指的方式滥用了 IE?

edit:基本上我正在尝试使用事件模型来捕获问题。以下代码使用 dispatchEvent出于同样的目的,在非 IE 浏览器中工作正常:

    if (document.createEvent) {
var event = document.createEvent("HTMLEvents");
event.initEvent("submit", false, false);
var returnValue = document.forms['fireForm'].dispatchEvent(event);

alert("event returnValue: " + returnValue);

}

最佳答案

来自 http://www.howtocreate.co.uk/tutorials/javascript/domevents

Note that manually firing an event does not generate the default action associated with that event. For example, manually firing a focus event does not cause the element to receive focus (you must use its focus method for that), manually firing a submit event does not submit a form (use the submit method), manually firing a key event does not cause that letter to appear in a focused text input, and manually firing a click event on a link does not cause the link to be activated, etc.

fireEvent 方法不会提交您的表单; 它将触发附加到该事件的任何事件处理程序。表单的提交方法将提交表单。

这是你想做的吗?

function fireSubmit () {
var fired = document.forms['fireForm'].submit();
alert("event fired: " + fired + "; event returnValue: " + event.returnValue);
}

关于javascript - 如何让 fireEvent 方法在 IE8 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com