简体   繁体   English

停止传播不适用于可点击div中的链接

[英]Stop Propagation not working for link inside clickable div

I've got a link that needs to be clicked situated inside a div that already has a function on click which I've emulated here . 我有一个需要单击的链接,位于一个div内,该链接已经具有我在此处模拟的单击功能。

Now in my code, the link that needs to be clicked has class remove-tag which is injected on running the script to the outer element search-box . 现在在我的代码中,需要单击的链接具有类remove-tag ,该类在运行脚本时注入到外部元素search-box

Referencing the earlier SO question of: 参考前面的SO问题:

jquery: keep <a> link clickable in clickable div jQuery:在可点击的div中保持<a>链接可点击

I applied similar logic to my code here: 我在这里对代码应用了类似的逻辑:

$('.remove-tag').click(function(event){
    event.stopImmediatePropagation();
    $(this).parent().remove();
});

I also tried event.stopPropagation(); 我也尝试过event.stopPropagation(); , but this doesn't work and clicking on the link still performs the function of the outer div which is technically a drop down sort of system. ,但这不起作用,并且单击链接仍然可以执行外部div的功能,从技术上讲,它是一种下拉式系统。

Ok, here we go: 好的,我们开始:

$('.search-box').on('click', '.remove-tag', function(event){
    event.stopPropagation();
    $(this).parent().remove();
});

JSFIDDLE: http://jsfiddle.net/snr9D/4/ JSFIDDLE: http : //jsfiddle.net/snr9D/4/

Since you are adding the element later to the DOM, you need the delegated handler , to bind the event to that. 由于稍后将元素添加到DOM中,因此需要委托处理程序来将事件绑定到该DOM。

The stopImmediatePropagation is working fine, the real problem is because your click event handler is not fired. stopImmediatePropagation正常运行,真正的问题是因为您的click事件处理程序未触发。

Is not fired because you are adding dynamically your elements to the DOM. 未触发,因为您正在向DOM动态添加元素。

In this case you have to use event delegation with jQuery on . 在这种情况下,您必须在jQuery on使用事件委托。

Ref: 参考:

The majority of browser events bubble, or propagate, from the deepest, innermost element (the event target) in the document where they occur all the way up to the body and the document element. 大多数浏览器事件从文档中最深的最内层元素(事件目标)起泡或传播,一直发生到正文和文档元素。 In Internet Explorer 8 and lower, a few events such as change and submit do not natively bubble but jQuery patches these to bubble and create consistent cross-browser behavior. 在Internet Explorer 8及更低版本中,诸如更改和提交之类的一些事件本身并不是冒泡的,但是jQuery修补了这些事件以冒泡并创建一致的跨浏览器行为。

If selector is omitted or is null, the event handler is referred to as direct or directly-bound. 如果选择器省略或为null,则事件处理程序称为直接或直接绑定。 The handler is called every time an event occurs on the selected elements, whether it occurs directly on the element or bubbles from a descendant (inner) element. 每当事件在选定元素上发生时,无论事件是直接发生在元素上还是来自后代(内部)元素的气泡,都将调用处理程序。

When a selector is provided, the event handler is referred to as delegated. 提供选择器后,事件处理程序称为“委托”。 The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. 当事件直接发生在绑定元素上时,不调用处理程序,而仅对与选择器匹配的后代(内部元素)进行调用。 jQuery bubbles the event from the event target up to the element where the handler is attached (ie, innermost to outermost element) and runs the handler for any elements along that path matching the selector. jQuery使事件从事件目标一直冒泡到附加了处理程序的元素(即,最内层元素到最外层元素),并沿该路径运行与选择器匹配的任何元素的处理程序。

Code: 码:

$('.search-box').on('click','.remove-tag',function (event) {
    event.stopImmediatePropagation();
    $(this).parent().remove();
});

Demo: http://jsfiddle.net/M3HdC/ 演示: http//jsfiddle.net/M3HdC/

You are not binding the click event to the .remove-tag elements when they are created. 创建.remove-tag元素时,未将click事件绑定到它们。

Just run this in the method where you append the elements to the body: 只需在将元素追加到主体的方法中运行此命令即可:

$('.remove-tag').click(function(event){
    event.stopImmediatePropagation();
    $(this).parent().remove();
});

JSFiddle JSFiddle

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

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