如何使用jQuery触发鼠标移动事件

5

我试图使用jQuery手动触发mousemove事件。请看这个示例http://jsfiddle.net/qJJQW/

从其他类似的Stack Overflow帖子上看,应该可以工作,为什么它不起作用呢?

2个回答

4
使用jQuery绑定mousemove事件:
$(function () {
   $("#test").on("mousemove", youCantHandleTheFunc);

    $('#button').click(function () {
        $('#test').trigger('mousemove', {type:'custom mouse move'});
    });
});

function youCantHandleTheFunc (e,customE) {
    if (customE != undefined) {
         e = customE;   
    }
    $('#result').html(e.type);
}

您更新的代码示例。


谢谢,非常感谢。我本以为是这样的,但在jQuery文档中发现了这行代码:““要触发通过jQuery绑定的处理程序而不触发本机事件,请改用.triggerHandler()。”这告诉我替代函数trigger()确实会触发本机事件(也许我对本机事件的理解有限)。 - hofnarwillie

4
jQuery的trigger()只能触发使用jQuery设置的事件处理程序吗?
$(function(){
    $('#test').on('mousemove', youCantHandleTheFunc); 

    $('#button').click(function(){
        $('#test').trigger('mousemove',{type:'custom mouse move'});
    });
});

function youCantHandleTheFunc(e,customE){
    if (customE!=undefined){
         e=customE;   
    }
    $('#result').html(e.type);
}

FIDDLE


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接