问题描述
出于好奇 - jQuery的 triggerHandler
的用途/用途是什么?据我所知,在触发
和 triggerHandler
之间的唯一真实差异就是本机事件火灾和事件冒泡行为(尽管 triggerHandler
的冒泡行为似乎难以用触发器
复制几行代码)。确保本地事件不发生的优势是什么?
Out of curiosity -- what is the purpose of / use cases for jQuery's triggerHandler
? As far as I can tell, the only "real" differences between trigger
and triggerHandler
is whether or not the native event fires, and event bubbling behavior (though triggerHandler
's bubbling behavior doesn't seem hard to replicate with trigger
in a few more lines of code). What is the advantage to ensuring the native event does not fire?
如果这是一个方便的功能或者存在更深的原因,我很好奇,为什么/当我使用它时。
I'm curious if this is a convenience function or there's a deeper reason it exists, and what why/when I would use it.
推荐答案
从
- .triggerHandler( )方法不会导致
事件的默认行为(如
表单提交)。
不阻止默认浏览器操作允许您指定应用样式的焦点或选择等等上发生的操作。也许你有一个基于Javascript的动态菜单,所以你不想纯粹用CSS应用风格,否则那些禁用Javascript的人不会明白为什么布局看起来很奇怪。您可以使用类似 $('menu1select')的内容。triggerHandler('click');
Not preventing the default browser actions allow you to specify an action that occurs on focus or select, etc etc etc, that applies a style. Maybe you have a dynamic menu that is Javascript based, so you don't want to apply the style purely with CSS otherwise those with Javascript disabled won't understand why the layout looks odd. You can use something like $('menu1select').triggerHandler('click');
如果您有一个隐藏元素onclick的事件,例如,你想通常调用该函数必须指定每个元素,您可以使用 $('。menu')。triggerHandler('click')
;
If you have an event which hides an element onclick for example, and you want to call that function generally, instead of having to specify each element, you can use $('.menu').triggerHandler('click')
;
- 使用.triggerHandler()创建的事件不会浮起
DOM层次结构;如果他们没有直接由目标元素处理
,他们
什么都不做。
防止传播,可笑的是不必解释这一个...
Prevents propagation, hopyfully don't have to explain this one...
应该是自我解释的...
This one should be self explanatory as well...
这篇关于triggerHandler与jQuery中的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!