如何从jQuery触发鼠标移动事件

时间:2013-10-22 10:43:14

标签: javascript jquery html javascript-events mousemove

我正在尝试使用jQuery手动触发mousemove事件。这个小提琴的演示http://jsfiddle.net/qJJQW/

来自stackoverflow上的其他类似帖子似乎应该可行。为什么不呢?

谢谢大家。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

使用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);
}

Your updated fiddle.