2016-02-22 63 views
0

我有包含锚标记删除特定李。 所以,我有onclick事件为锚标记和li。点击李打开一个模式窗口,点击锚标签应该隐藏李。防止点击事件触发父行动

如何触发锚点标记上的点击事件,而不触发点击事件。 这里是我已经厌倦至今:

JS:

$('body').on('click', 'a', function(e) { 
    if(confirm('Are you sure you want to delete it?') == true){ 
     $(this).parent().hide(); 
    } 
    //e.preventDefault();  
    e.stopPropagation(); 
}); 

HTML:

<li onclick="openModal(0)"> 
    <a class="closer">X</a> 
    <h3>Lolita</h3> 
    <p>Check it</p> 
</li> 
+1

[关于()stopPropagation不工作jQuery的?]的可能的复制(http://stackoverflow.com/questions/8995008/jquery-on-stoppropagation-not-working) – Script47

+0

也许你可以张贴代码点击'li'就会被执行。我复制了你的代码,点击'a'后它不会触发'li'点击。 – Pit

回答

1

这里是你的答案小提琴:https://jsfiddle.net/jimedelstein/bsg4jq3m/

您需要检测的目标的点击,如果它的链接忽略它,否则触发li点击。

<li> 
    <a class="closer">X</a> 
    <h3>Lolita</h3> 
    <p>Check it</p> 
</li> 


$('body').on('click', 'a', function(e) { 
    if(confirm('Are you sure you want to delete it?') == true){ 
     $(this).parent().hide(); 
    } 
    e.stopPropagation(); 
}); 

function openModal(e){ 
    if (e.target != $("a.closer")[0]){ 
    alert("not the link!"); 
    } 
}; 

$("li").on("click", openModal); 
+0

thanxxx a ton mate ...它的工作原理!! 1 –

相关问题