Firefox中的Firefox 5 dispatchEvent [英] Firefox 5 dispatchEvent in Firefox
问题描述
我有一些使用 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中可以很好地加载,但是当我在Firebug中运行此代码甚至将其作为书签执行时,Firefox绝对不执行任何操作.我还尝试通过设置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;
}
请参见我还用法语编写了有关引发DOM事件的教程;我想很容易将其翻译.
I also wrote a tutorial in French language about firing DOM events ; I guess it's easy to get it translated.
这篇关于Firefox中的Firefox 5 dispatchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!