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

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

问题描述

出于好奇 - 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.

推荐答案

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



  • 而不是返回jQuery对象(允许链接),
    .triggerHandler()返回由最后一个处理程序
    返回的
    值。如果没有
    处理程序被触发,它返回
    未定义

应该是自我解释的...

This one should be self explanatory as well...

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

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