javascript jquery 中的 e.stopPropagation() 是否适用于锚标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8952176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:07:54  来源:igfitidea点击:

does e.stopPropagation() in jquery works on anchor tag

javascriptjquery

提问by spiderman

i want prevent eventpropogation from element inside an anchor tag i m trying to use e.stopPropagation(); it doesn't seem to work is it possible or i m just wasting time please help me out of here javascript is:

我想阻止来自锚标签内元素的事件传播,我正在尝试使用 e.stopPropagation(); 它似乎不起作用是否可能或我只是在浪费时间请帮助我离开这里javascript是:

$(document).ready(function(){
    $('.alink .p').click(function(e){
    alert("hi");
    e.stopPropagation();  
});

html is :

html是:

<div>
    <a href="http://google.com" class="alink" >Google links
          <p class="p">Another Links to prevent default</p>
    </a>
</div>

thanks for your precious time

感谢您的宝贵时间

回答by Krzysztof Bujniewicz

event.stopPropagation()stops passing the event to handlers further away in DOM structure from the element, on which originally event was triggered. It does not, although, prevent action that has already been triggered.

event.stopPropagation()停止将事件传递给 DOM 结构中离元素更远的处理程序,在该元素上触发了最初的事件。但是,它不会阻止已触发的操作。

You should use event.preventDefault()to stop mentioned above default action.

您应该使用event.preventDefault()停止上述默认操作。

Sources:

资料来源:

回答by dknaack

Description

描述

It will not stop any default behaviours (such as link clicks) and you might want to consider using event.preventDefault()in addition to this method.

event.stopPropagation()is only for event handlers, not default behavior.

event.preventDefault()If this method is called, the default action of the event will not be triggered.

它不会停止任何默认行为(例如链接点击,除了此方法之外,您可能还需要考虑使用event.preventDefault()

event.stopPropagation()仅用于事件处理程序,而不是默认行为。

event.preventDefault()如果调用此方法,则不会触发事件的默认动作。

You had some spelling errors in your script and its documentnot "document".

您的脚本中有一些拼写错误,而document没有"document"

Check out the Sample and this jsFiddle Demonstration

查看示例和此jsFiddle 演示

Sample

样本

$(document).ready(function(){
    $('.alink, .alink > .p').click(function(e){
        e.preventDefault();
        e.stopPropagation();
        alert("hi");
    });
});

More Information

更多信息

回答by Dau

try this

试试这个

$(document).ready(function(){
    $('.alink .p').click(function(e){
        e.preventDefault();
        e.stopImmediatePropagation();
        alert("hi");
        return false;
    });
});

回答by Vivek

you should use- e.preventDefault()to stop default behavior. stopPropagation is used to stop event bubbling.

您应该使用-e.preventDefault()来停止默认行为。stopPropagation 用于停止事件冒泡。

回答by gregolsen

You've missed enclosing brackets here, didn't you?

你错过了这里的括号,不是吗?

$('document').ready(function(){
    $('.alink .p').click(function(e){
    alert("hi");
    e.stopPropagation();  
});

Fix brackets and then use e.preventDefault() instead.

修复括号,然后使用 e.preventDefault() 代替。