我目前正在尝试在点击时更改子元素的颜色。父元素链接到另一个页面。我希望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();
});
如果重要,锚标签围绕祖父母。
我哪里错了?
答案 0 :(得分:1)
$('.child').click(function(e) {
if($(this).hasClass('active')){
$(this).removeClass('pinned');
}
else {
$(this).addClass('pinned');
}
e.stopPropagation();
return false;
});
您可以在同一功能中使用e.stopPropagation
。