2013-02-22 74 views
0

我目前正試圖在點擊時更改子元素的顏色。父元素鏈接到另一個頁面。我希望stopPropogation會起作用,但事實並非如此。以下是我的jQuery代碼:StopPropogation - 無法禁用父錨定標記

$('.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
$('.child').click(function(e) { 
    if($(this).hasClass('active')){ 
     $(this).removeClass('pinned'); 
    } 
    else { 
     $(this).addClass('pinned'); 
    } 
    e.stopPropagation(); 
    return false; 
}); 

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

+0

謝謝!我可以在2分鐘內得到答案。 :) – jstacks 2013-02-22 04:44:37