StopPropagation - 无法禁用父级锚标记

时间:2013-02-22 04:31:38

标签: jquery click stoppropagation

我目前正在尝试在点击时更改子元素的颜色。父元素链接到另一个页面。我希望stopPropogation可行,但事实并非如此。以下是我的jQuery代码:

$('.child').click(function(){

    if($(this).hasClass('pinned')){

        $(this).removeClass('pinned')

    }

    else{

        $(this).addClass('pinned')

    }

}); 

    // disable parent link when pinning

    $('.child').click(function(event){

        event.stopPropagation();

    }); 

如果重要,锚标签围绕祖父母。

我哪里错了?

1 个答案:

答案 0 :(得分:1)

$('.child').click(function(e) {
    if($(this).hasClass('active')){
        $(this).removeClass('pinned');
    }
    else {
        $(this).addClass('pinned');
    }
    e.stopPropagation();
    return false;
}); 

您可以在同一功能中使用e.stopPropagation

相关问题