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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19515612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:51:09  来源:igfitidea点击:

How to trigger a mouse move event from jQuery

javascriptjqueryhtmljavascript-eventsmousemove

提问by hofnarwillie

I'm trying to manually trigger a mousemoveevent with jQuery. Demo in this fiddle http://jsfiddle.net/qJJQW/

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

From other similar posts on stackoverflow it seems that this should work. Why isn't it?

从 stackoverflow 上的其他类似帖子看来,这应该可行。为什么不是?

Thanks all.

谢谢大家。

采纳答案by karim79

Use jQuery to bind the mousemove event:

使用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.

你更新的小提琴。

回答by adeneo

jQuery's trigger()only triggers event handlers set with jQuery ?

jQuerytrigger()只触发用 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

小提琴