简体   繁体   English

Jquery停止触发事件

[英]Jquery stop event from triggering

I have simple scenario as shown in fiddle which is 我有简单的场景,如小提琴所示

Initially event is binded on only one button A and click event is triggered in both the buttons(A & B) manually. 最初事件仅在一个按钮A上binded ,并且手动在按钮(A和B)中触发click事件。

Now in event handler of button A the event handler for button B is binded. 现在在按钮A的事件处理程序中,按钮B的事件处理程序被绑定。 It is expected not to execute the callback of B as it is binded after the event is triggered. 预计不会执行B的回调,因为它在事件被触发后被绑定。

But the callback is still executed. 但回调仍然执行。 Where am i going wrong? 我哪里错了?

HTML: HTML:

<div>
    <button class="a">botton A</button>
    <button class="b">button B</button>
</div>

JS: JS:

$(function () {
    $('div').on('click', '.a', function (e) {
        e.preventDefault();
        $('div').on('click', '.b', function () {
            alert('Can"t see me')
        });
    });
    $('button').trigger('click');
});

FIDDLE LINK FIDDLE LINK

EDIT: 编辑:
I have this scenario in my project and now I knew why is this happening :). 我在我的项目中有这个场景,现在我知道为什么会发生这种情况:)。 But how can i stop event from propagating? 但是我如何阻止事件传播?

$('button').trigger('click'); triggers a click event on both buttons in the order they appear in the document markup. 按两个按钮在文档标记中出现的顺序触发单击事件。 Since event bubbling is synchronous, the event for .b is binded before the click event is triggered on that button. 由于事件冒泡是同步的,因此.b的事件在该按钮上触发click事件之前被绑定。 Here's the breakdown: 这是细分:

  1. $('button') creates a collection of button .a and button .b . $('button')创建按钮.a和按钮.b的集合。
  2. .trigger('click'); iterates through each button in the collection and generates a click event. 遍历集合中的每个按钮并生成单击事件。
  3. button .a receives the click event and runs its event handler you registered for it on page load. 按钮.a接收click事件并运行您在页面加载时为其注册的事件处理程序。
  4. The event handler for .b is registered within the callback of the event handler for .a . .b的事件处理程序在.a的事件处理程序的回调中注册。
  5. button .b receives the click event from .trigger('click'); 按钮.b.trigger('click');接收点击事件.trigger('click'); since it's second in the collection. 因为它是该系列的第二名。
  6. The callback of the event listener for button .b triggers the popup alert. 按钮.b的事件侦听器的回调会触发弹出警报。

Since you only want to trigger button .a to be clicked, any of the following will work on your current document: 由于您只想触发单击按钮.a ,因此以下任何一项都适用于您当前的文档:

  • $('.a').trigger('click');
  • $('button.a').trigger('click'); (though this is redundant) (虽然这是多余的)
  • $('button').get(0).trigger('click'); (since .a is the 0th indexed button element) (因为.a是第0个索引按钮元素)

EDIT Although this is unrelated to the question, Perhaps you meant to register the event for .b only once, doing this: 编辑虽然这与问题无关,但也许你只想为.b注册一次事件,这样做:

$('.a').one('click', function (e) {

 $(function() { $('.a').one('click', function(e) { e.preventDefault(); $('div').on('click', '.b', function() { alert('Can\\'t see me'); }); }); $('.a').trigger('click'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <button class="a">button A</button> <button class="b">button B</button> </div> 

SECOND EDIT If you want to stop event propagation, you need to add another event listener, like this: 第二次编辑如果要停止事件传播,则需要添加另一个事件侦听器,如下所示:

 $(function() { $('.a').one('click', function() { $('.b').on('click', function(e) { e.stopPropagation(); alert('Can see me'); }); $('div').on('click', '.b', function() { alert('Can\\'t see me'); }); }); $('button').trigger('click'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <button class="a">button A</button> <button class="b">button B</button> </div> 

It's because of $('button').trigger('click'); 这是因为$('button').trigger('click'); .

This triggers a click on every button - first it clicks on .a , which binds the click handler to .b , then it clicks on .b . 这会触发每个按钮的点击 - 首先它点击.a ,它将点击处理程序绑定到.b ,然后点击.b

In my experiment, simply putting button .b first fixed this, but I wouldn't rely on that. 在我的实验中,只需按下按钮.b首先修复此问题,但我不会依赖它。

This is probably because the click is being triggered on all button instances serially... 这可能是因为在所有按钮实例上串行触发了点击...

button a click gets executed first...in its callback you bind button b click callback... button a点击首先执行...在其回调中绑定button b单击回调...

...then... button b click gets executed...hence callback for b gets executed... ...然后... button b点击执行...因此b的回调被执行...

All of this happens in that one call: $('button').trigger('click'); 所有这一切都发生在一次调用中: $('button').trigger('click');

Edit: others have probably answered this before I have...but to iterate trigger the click on the correct element : 编辑:其他人可能在我有...之前已经回答了这个问题但是迭代触发点击正确的元素:

$('.a').trigger('click');

The order of the execution is: 执行顺序是:

  • bind .a 绑定.a
  • trigger first button (say, a) 触发第一个按钮(例如,a)
  • callback for a: bind b 回调a:bind b
  • trigger second button (b) 触发第二个按钮(b)
  • callback for b 回调b

Even $("button").trigger("click") is a single line, it is a kind of loop , works like if it were: 甚至$("button").trigger("click")是一行,它是一种循环 ,就像它是这样的:

var buttonElms = $("button");
for (var index in buttonElms) {
   buttonElms[index].trigger("click");
}

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

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