在 JavaScript 中,什么是 event.isTrigger?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10704168/
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
In JavaScript, what is event.isTrigger?
提问by djb
I came across this in some JS code I was working on:
我在我正在处理的一些 JS 代码中遇到了这个:
if ( typeof( e.isTrigger ) == 'undefined' ) {
// do some stuff
}
This seems to be part of jQuery. As far as I can see it tells you if an event originated with the user or automatically.
这似乎是 jQuery 的一部分。据我所知,它会告诉您事件是由用户发起还是自动发起。
Is this right? And given that it's not documented, is there a way of finding such things out without going behind the curtain of the jQuery API?
这是正确的吗?鉴于它没有记录,有没有办法在不隐藏 jQuery API 的情况下找到这些东西?
采纳答案by Snuffleupagus
In jQuery 1.7.2 (unminified) line 3148 contains event.isTrigger = true;
nested within the trigger function. So yes, you are correct - this is only flagged when you use .trigger()
and is used internally to determine how to handle events.
在 jQuery 1.7.2(未缩小)第 3148 行包含event.isTrigger = true;
嵌套在触发器函数中。所以是的,您是对的 - 这仅在您使用时被标记,.trigger()
并在内部用于确定如何处理事件。
回答by Nipuna
If you look at jQuery github project, inside trigger.js file line 49 (link here) you can find how isTrigger gets calculated.
如果您查看 jQuery github 项目,在 trigger.js 文件第 49 行(此处链接)中,您可以找到 isTrigger 的计算方式。
If you add a trigger in your JavaScript and debug through, You can see how the breakpoint reaches this codeline (checked in jQuery-2.1.3.js for this SO question)
如果您在 JavaScript 中添加触发器并调试通过,您可以看到断点如何到达此代码行(在 jQuery-2.1.3.js 中检查此 SO 问题)
回答by ilyaigpetrov
Modern browsers fight against popup windows opened by automated scripts, not real users clicks. If you don't mind promptly opening and closing a window for a real user click and showing a blocked popup window warning for an automated click then you may use this way:
现代浏览器对抗由自动脚本打开的弹出窗口,而不是真正的用户点击。如果您不介意为真正的用户点击立即打开和关闭窗口,并为自动点击显示阻止的弹出窗口警告,那么您可以使用这种方式:
button.onclick = (ev) => {
// Window will be shortly shown and closed for a real user click.
// For automated clicks a blocked popup warning will be shown.
const w = window.open();
if (w) {
w.close();
console.log('Real user clicked the button.');
return;
}
console.log('Automated click detected.');
};