triggerHandler 与 jQuery 中的触发器 [英] triggerHandler vs. trigger in jQuery

查看:18
本文介绍了triggerHandler 与 jQuery 中的触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇——jQuery triggerHandler 的/用例的目的是什么?据我所知,triggertriggerHandler 之间唯一真正"的区别是本机事件是否触发,以及事件冒泡行为(尽管 triggerHandler 的冒泡行为似乎并不难在多几行代码中使用 trigger 复制).确保本机事件不会触发有什么好处?

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.

推荐答案

来自 http://api 的文档.jquery.com/triggerHandler/

.triggerHandler() 方法的行为与 .trigger() 类似,使用以下例外:

The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions:

  • .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');

  • 虽然 .trigger() 将对 jQuery 匹配的所有元素进行操作对象,.triggerHandler() 只影响第一个匹配的元素.

例如,如果您有一个隐藏元素 onclick 的事件,并且您希望通常调用该函数,而不必指定每个元素,则可以使用 $('.menu').triggerHandler('点击');

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

  • 而不是返回 jQuery 对象(以允许链接),.triggerHandler() 返回任何内容值由最后一个处理程序返回它导致被处决.如果不处理程序被触发,它返回未定义

这个也应该是不言自明的......

This one should be self explanatory as well...

这篇关于triggerHandler 与 jQuery 中的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆