简体   繁体   English

如何使它如果我单击一个<a>元素它转到链接,但它不会触发其父元素的.click()事件?</a>

[英]How do I make it so that if I click an <a> element it goes to the link, but it doesn't trigger the .click() event of its parent element?

I want to be able to click on a particular <a> and activate the link out, but not the parent's click. 我希望能够点击特定的<a>并激活链接,但不能激活父亲的点击。

preventDefault() doesn't work because then it won't link out. preventDefault()不起作用,因为它不会链接出来。

You should use e.stopPropagation() to stop the event from bubbling. 您应该使用e.stopPropagation()来阻止事件冒泡。

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. 防止事件冒泡DOM树,防止任何父处理程序被通知事件。

~from jQuery docs .stopPropagation 〜来自jQuery docs .stopPropagation

You have to use stopPropagation instead of preventDefault 您必须使用stopPropagation而不是preventDefault

$('a').click(event){
     event.stopPropagation();
});

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

相关问题 Javascript / AngularJS:如何使点击事件触发器成为父项? - Javascript/AngularJS: How do I make click event trigger parent? 如何进行AJAX调用,以便在我单击特定类中包含的链接时触发,并包含父元素的“id”? - How to make an AJAX call so that it only fires when I click on a link contained within a specific class, and includes the “id” of the parent element? 我似乎无法停止将click事件传播到父元素 - I can't seem to stop propagation of click event to parent element 如何在角度4中动态生成的元素上触发click事件? - How can I trigger a click event on element generated dynamically in angular 4? 如何触发列表元素的点击事件? - How can I trigger the click event on a list element? 如何在元素的特定后代上触发点击事件? - How can I trigger a click event on a specific descendent of an element? 如何在禁用的按钮元素上触发点击事件(使用纯香草JS)? - How do I trigger a click event on a disabled button element (using pure vanilla JS)? 具有相似行为的第二个元素上的JS click事件不会触发 - JS click event on second element with similar behavior doesn't trigger 元素触发点击事件不适用于离子 - Element trigger click event doesn't work with ionic 如何从父元素触发href单击事件 - How to trigger a href click event from the parent element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM