简体   繁体   English

如何使用.triggerhandler捕获委托事件

[英]how to catch delegated events with .triggerhandler

We attach our event handlers using jQuery for DOM elements either 我们使用jQuery为DOM元素附加我们的事件处理程序

1)directly — 1)直接 -

    $(#elemId).bind("click",function(){ …//event handler code}); OR

2) by delegation. 2)通过授权。 -

         $(document).delegate("#elemId","click",function(){ …//event handler code});

We also come accross a situtation whereby we need to execute these attached event handlers manually from our code. 我们也遇到了一个问题,我们需要从代码中手动执行这些附加的事件处理程序。 To do this we can use either .trigger() API in jQuery on the element. 为此,我们可以在元素的jQuery中使用.trigger()API。

       $(#elemId).trigger("click") 

In some scenarios we only want to execute the event handlers we attached but we do not want to execute the default action that is associated with that event. 在某些情况下,我们只想执行我们附加的事件处理程序,但我们不想执行与该事件关联的默认操作。 We can acheive this by using .triggerHandler() API in jQuery on element 我们可以通过在元素上的jQuery中使用.triggerHandler()API来实现这一点

$(#elemId).triggerHandler("click");

eg we can attach "click" event on a list of "checkboxes" and we can execute an event handler when any of the checkboxes are checked/unchecked. 例如,我们可以在“复选框”列表中附加“click”事件,并且当选中/取消选中任何复选框时,我们可以执行事件处理程序。 Here the default action of click event for a checkbox is that either that checkbox gets checked /uncehcked(ie that status of the checkbox changes.) So. 这里复选框的单击事件的默认操作是复选框被选中/未清除(即复选框的状态发生变化)。 in some scenarios we might not want to change the status of the checkbox when it is clicked but we would like to execute the code in our event handler.As explained aboce this can be achieved in jQuery by calling .triggerHandler() instead of .trigger() to fire the event. 在某些情况下,我们可能不希望在单击时更改复选框的状态,但我们希望在事件处理程序中执行代码。正如所解释的那样,这可以通过调用.triggerHandler()而不是.trigger在jQuery中实现。 ()发射事件。

However this does not work when we attach event to a DOM element by delegation. 但是,当我们通过委托将事件附加到DOM元素时,这不起作用。 What is the other way to execute event handlers without executing default action in case of event delegation? 在事件委托的情况下,执行事件处理程序而不执行默认操作的另一种方法是什么?

Please check the difference in following JSfiddles: for direct event handling : 请检查以下JSfiddles中的差异:对于直接事件处理:

       http://jsfiddle.net/g2TWh/7/

for delegated event handling : 用于委派事件处理:

        http://jsfiddle.net/g2TWh/8/ 

(Note that alert in event handler function does not appear when you click "Run") (请注意,单击“运行”时,不会出现事件处理函数中的警报)

According to JQuery API: 根据JQuery API:

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. 如果它们不是直接由目标元素处理的,它们什么都不做。

Because you use event delegation, the event can not bubble to document that suppose to handle the event 因为您使用事件委派,所以事件无法冒泡到假设处理事件的文档

The other way would be adding custom event and using event.preventDefault() in its handler: 另一种方法是添加自定义事件并在其处理程序中使用event.preventDefault():

    //attach an event "click" by delegation
$(document).on("customevent", "#childinput1", function (e) {
    e.preventDefault();
    alert('in delegated  "custom" event handler');
});
$(document).on("click", "#childinput1", function (e) {
    alert('in delegated  "click" event handler');
});
testTrigger();

function testTrigger() {
    $("#childinput1").trigger("customevent");
}

Fiddle 小提琴

By the way you if you use jquery 1.7+ you should could use "$.on" instead of "$.delegate": 顺便说一句,如果你使用jquery 1.7+,你应该使用“$ .on”而不是“$ .delegate”:

As of jQuery 1.7, .delegate() has been superseded by the .on() method. 从jQuery 1.7开始,.delegate()已被.on()方法取代。 For earlier versions, however, it remains the most effective means to use event delegation. 但是,对于早期版本,它仍然是使用事件委派的最有效方法。

One solution I found to this when I came across the same scenario was to check if the event was triggered, and if so, prevent the default action. 当我遇到相同的情况时,我发现的一个解决方案是检查事件是否被触发,如果是,则阻止默认操作。 This way rather than triggering the handler (which isn't possible with delegation as mentioned above), you can trigger the full event and just condition the action based on the event properties. 这种方式而不是触发处理程序(如上所述,这是不可能的委托),您可以触发完整事件,并根据事件属性调整操作。 Take this for example: 以此为例:

//attach an event "click" by delegation
$(document).delegate("#childinput1", "click.test", function (e) {
    // prevent default if this event was triggered
    if (!!e.isTrigger) {
     e.preventDefault();   
    }
    alert('in delegated  "click" event handler');
});
testTrigger();

function testTrigger() {
    $("#childinput1").trigger("click.test");
}

Here's the modified JSFiddle: http://jsfiddle.net/x9d9yjtv/ 这是修改后的JSFiddle: http//jsfiddle.net/x9d9yjtv/

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

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