Firefox 5 dispatchEvent不能正常工作 [英] Firefox 5 dispatchEvent not working
问题描述
我有一些使用 dispatchEvent
来模拟点击的代码,在Chrome中相同的代码工作正常,但在Firefox中不起作用。以下是代码:
I have some code that uses dispatchEvent
to simulate clicks and the same exact code works fine in Chrome but doesn't work in Firefox. Here's the code:
var evt = document.createEvent("MouseEvents");
evt.initEvent("click",true,true);
jQuery("a:contains(Next)")[0].dispatchEvent(evt);
我点击一个加载另一个页面的链接,页面在Chrome中正常加载,但Firefox当我在Firebug中运行这个代码,甚至当我将它作为一个书签执行时,绝对没有任何东西。我也尝试了通过设置MDC文档中显示的所有选项来初始化事件的长形式,但是这并没有做任何事情。到底什么是我在这里做错了?
I'm clicking on a link that loads another page and the page loads fine in Chrome but Firefox does absolutely nothing when I run this code in Firebug or even when I execute it as a bookmarklet. I've also tried the long form of event initializing by setting all the options as shown on the MDC docs but that doesn't do anything. What exactly am I doing wrong here?
推荐答案
由于你的事件看起来像一个鼠标事件,你可以尝试使用鼠标事件,就像这个例子:
As your event looks to be a mouse event, you may rather try using a mouse event, like this example :
var oEvt = (document.createEvent)? document.createEvent('MouseEvents') : document.createEventObject();
// W3C
if (oEvt.initMouseEvent)
oEvt.initMouseEvent(
/* type*/ 'mouseup',
/* bubble*/ true,
/* cancel*/ true,
/* AbstractView*/ window,
/* detail */ 10,
/* screenX */ 20,
/* screenY */ 30,
/* clientX */ 40,
/* clientY */ 50,
/* ctrlKey */ false,
/* altKey */ false,
/* shiftKey */ true,
/* metaKey */ false,
/* button */ 0,
/* relatedTarget*/ null ) ;
// MSIE
else {
var oEvt = document.createEventObject();
oEvt.detail = 10;
oEvt.screenX = 20;
oEvt.screenY = 30;
oEvt.clientX = 40;
oEvt.clientY = 50;
oEvt.ctrlKey = false;
oEvt.altKey = false;
oEvt.shiftKey = true;
oEvt.metaKey = false;
oEvt.button = 0;
oEvt.relatedTarget = null;
}
请参阅 W3C鼠标事件类型
这篇关于Firefox 5 dispatchEvent不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!