检查事件是否由人触发 [英] Check if event is triggered by a human
本文介绍了检查事件是否由人触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个附加到事件的处理程序,我希望它仅在由人触发而不是由trigger()方法触发时执行.我该如何区分?
I have a handler attached to an event and I would like it to execute only if it is triggered by a human, and not by a trigger() method. How do I tell the difference?
例如,
$('.checkbox').change(function(e){
if (e.isHuman())
{
alert ('human');
}
});
$('.checkbox').trigger('change'); //doesn't alert
推荐答案
您可以检查e.originalEvent
:如果已定义,则单击是人为的:
You can check e.originalEvent
: if it's defined the click is human:
看看小提琴 http://jsfiddle.net/Uf8Wv/
$('.checkbox').change(function(e){
if (e.originalEvent !== undefined)
{
alert ('human');
}
});
我在小提琴中的例子:
<input type='checkbox' id='try' >try
<button id='click'>Click</button>
$("#try").click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
$('#click').click(function(event) {
$("#try").click();
});
这篇关于检查事件是否由人触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文