简体   繁体   English

触发器在jQuery中触发器与触发器

[英]triggerHandler vs. trigger in jQuery

Out of curiosity -- what is the purpose of / use cases for jQuery's triggerHandler ? 出于好奇 - jQuery的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). 据我所知, triggertriggerHandler之间唯一的“真正”区别在于本机事件是否触发,以及事件冒泡行为(尽管triggerHandler的冒泡行为似乎难以用trigger复制更多内容代码行)。 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. 我很好奇这是否是一个便利功能,或者它存在更深层次的原因,以及我何时/什么时候使用它。

From the Docs at http://api.jquery.com/triggerHandler/ 来自http://api.jquery.com/triggerHandler/上的文档

The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions: .triggerHandler()方法的行为与.trigger()类似,但有以下例外:

  • The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission). .triggerHandler()方法不会导致事件的默认行为(例如表单提交)。

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. 也许你有一个基于Javascript的动态菜单,所以你不想纯粹用CSS应用这种风格,否则那些禁用了Javascript的人将无法理解为什么布局看起来很奇怪。 You can use something like $('menu1select').triggerHandler('click'); 您可以使用$('menu1select').triggerHandler('click');

  • While .trigger() will operate on all elements matched by the jQuery object, .triggerHandler() only affects the first matched element. 虽然.trigger()将对jQuery对象匹配的所有元素进行操作,但.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') ; 例如,如果你有一个隐藏元素onclick的事件,并且你想要通常调用该函数,而不是必须指定每个元素,你可以使用$('.menu').triggerHandler('click') ;

  • Events created with .triggerHandler() do not bubble up the DOM hierarchy; 使用.triggerHandler()创建的事件不会冒泡DOM层次结构; if they are not handled by the target element directly, they do nothing. 如果它们不是直接由目标元素处理的,它们什么都不做。

Prevents propagation, hopyfully don't have to explain this one... 防止传播,hopyfully不必解释这个...

  • Instead of returning the jQuery object (to allow chaining), .triggerHandler() returns whatever value was returned by the last handler it caused to be executed. .triggerHandler()不返回jQuery对象(允许链接),而是返回它导致执行的最后一个处理程序返回的任何值。 If no handlers are triggered, it returns undefined 如果没有触发处理程序,则返回undefined

This one should be self explanatory as well... 这个也应该是自我解释的......

What is the advantage to ensuring the native event does not fire? 确保本机事件不会触发有什么好处?

  • You have actions bound to a 'focus' event but you don't want the browser to focus really focus it (might seem dumb but it could happen, couldn't it? like a code that you would like to execute once without losing the current focus). 你有动作绑定到'焦点'事件,但你不希望浏览器专注于真正关注它(可能看起来很愚蠢,但它可能会发生,不是吗?就像你想要执行一次而不会丢失的代码目前的焦点)。

  • A component that you are making want to trigger 'load' (just an example of a generic thing) of another component that is inside it. 您正在制作的组件想要触发其中的另一个组件的“加载”(只是一个通用事物的示例)。

    In that case, if you are calling 'load' of children when 'load' of the parent comes, you don't want to do this because it would cause an infinite call if the event.stopPropagation isn't called by the listeners of 'load' event (caused by bubling): 在这种情况下,如果你在父级的'load'时调用子级的'load',你不想这样做,因为如果event.stopPropagation没有被调用者调用,它将导致无限调用'load'事件(由起泡引起):

 $container.on('load', function () { $somethingInsideContainer.trigger('load'); // Would cause a loop if no event.stopPropagation() is called }); 

In that case you have to call triggerHandler(). 在这种情况下,您必须调用triggerHandler()。

Difference 1: you can call all elements matched by the JQuery object using trigger. 差异1:您可以使用触发器调用JQuery对象匹配的所有元素。

//Example1 for trigger. //触发器的示例1 All 3 button click events are fired when used trigger. 使用触发器时会触发所有3个按钮单击事件。 //Try Replacing trigger method with triggerHandler(). //尝试用triggerHandler()替换触发器方法。 You will see only the first button element event handler will fire . 您将看到只有第一个按钮元素事件处理程序将触发。

<button id = "button1">button1</button>
<button id = "button2">button2</button>
<button id = "button3">button3</button>

$("#button1").on("click", function(){
alert("button1 clicked");
});
$("#button2").on("click", function(){
alert("button2 clicked");
});
$("#button3").on("click", function(){
alert("button3 clicked");
});

//substitute trigger with triggerHandler to see the difference //用triggerHandler替换触发器以查看差异

$("#button1, #button2, #button3").trigger("click");

Difference 2: when using triggerHandler() for an element event, the native event will not be called for that element. 差异2:当对元素事件使用triggerHandler()时,不会为该元素调用本机事件。 trigger() will work fine. trigger()会正常工作。

//Example: //例:

//substitute trigger with triggerHandler to see the difference //用triggerHandler替换触发器以查看差异

 <button id = "button1">button1</button>
  <button id = "button2">button2</button>

$("#button1").on("click", function(){
 $("#button2").trigger('click');

});

$("#button3").on("click", function(){
var value = $("#button2").triggerHandler('click');
    alert('my value:'+ value)
});

$("#button2").on('click', function(){
alert("button2 clicked");

});

Difference 3: trigger() return Jquery object whereas triggerHandler() return the last handle value or If no handlers are triggered, it returns undefined 差异3: trigger()返回Jquery对象,而triggerHandler()返回最后一个句柄值,或者如果没有触发处理程序,则返回undefined

//Example3 //示例3

<button id="button1">Button1</button>
<button id="button2">Button2</button>
<button id="button3">Button3</button>


$("#button1").on("click", function(){
var myValue = $("#button2").trigger('click');
    alert(myValue);
});

$("#button3").on("click", function(){
var value = $("#button2").triggerHandler('click');
    alert('my value:'+ value)
});

$("#button2").on('click', function(){
alert("button2 clicked");
    return true;
});

Other difference is 其他的区别是

Events triggered with triggerHandler() do not bubble up the DOM hierarchy; 使用triggerHandler()触发的事件不会冒出DOM层次结构; if they are not handled by the target element directly, they do nothing. 如果它们不是直接由目标元素处理的,它们什么都不做。

For me the main difference is that 'triggerHandler' returns whatever was returned by the last handler, whereas 'trigger' returns the jQuery object. 对我来说,主要的区别是'triggerHandler'返回最后一个处理程序返回的内容,而'trigger'返回jQuery对象。

So, for a handler such as: 因此,对于处理程序,例如:

  $( document ).on( 'testevent', function ()
  {
    return false;
  });

Using 'triggerHandler' you can do the following: 使用'triggerHandler'可以执行以下操作:

  if( $( document ).triggerHandler( 'testevent' ) === false )
  {
    return;
  }

So you would use 'triggerHandler' if you wanted to respond to the result returned from the handler. 因此,如果要响应处理程序返回的结果,可以使用“triggerHandler”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM