当使用addEventListener()添加mousemove处理程序时,永远不会调用处理程序 使用$(xx).mousemove()或$(xx).trigger(e)的JQuery模拟事件,其中e是jquery事件。 但是,当使用纯JS dispatchEvent模拟事件时,可以调用侦听器。 有人可以解释一下吗?我的环境是Mac + chrome。
代码在这里http://jsfiddle.net/eepaul/r8W2h/
<body>
<ul id="id_ul">
<li id="a">oooo</li>
<li id="b">jjjj</li>
</ul>
<p id="console"></p>
</body>
JS
var liA = $("li#a")[0];
var ul = $("ul")[0];
var p = $("p#console")[0];
ul.addEventListener("mousemove", function(e) {
$(p).text($(p).text() + "mousemove triggered\n");
}, false);
var event = $.Event("mousemove", {
canBubble:true,
cancelable: true,
view:liA.ownerDocument.defaultView,
detail: 1,
screenX:0, //The coordinates within the entire page
screenY: 0,
clientX: 0, //The coordinates within the viewport
clientY: 0,
ctrlKey:false,
altKey:false,
shiftKey: false,
metaKey:false, //I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though!
button: 0, //0 = left, 1 = middle, 2 = right
relatedTarget:null
});
//neither of the following 2 ways can trigger the handler
window.setTimeout(function() {
$(liA).trigger(event);}, 1000);
window.setTimeout(function() {
$(liA).mousemove();}, 1000);