Jquery单击事件传播 [英] Jquery click event propagation
问题描述
我有一个表与点击事件绑定到其行(< tr>
)。这些行内有< a>
元素,其中分配了自己的点击事件。
I have a table with click events bound to its rows (<tr>
). There are <a>
elements inside those rows with their own click events assigned.
问题是,当我点击< a>
元素,它还会从父< tr>
中触发点击事件。我不想这样的行为;我只想点击< a>
点击事件。
Problem is, when i click on the <a>
element, it also fires the click event from the parent <tr>
. I don't want this behavior; I just want to fire the <a>
click event.
代码:
// Event row TR
$("tr:not(:first)").click(function() {
$(".window, .backFundo, .close").remove();
var position = $(this).offset().top;
position = position < 0 ? 20 : position;
$("body").append( $("<div></div>").addClass("backFundo") );
$("body").append( $("<div></div>").addClass("window")
.html("<span class=close><img src=Images/close.png id=fechar /></span>")
.append( "<span class=titulo>O que deseja fazer?</span>"
+"<span class=crud><a href=# id=edit>Editar</a></span>"
+"<span class=crud><a href=# id=delete codigo="
+ $(this).children("td:first").html()
+ ">Excluir</a></span>" )
.css({top:"20px"})
.fadeIn("slow") );
$(document).scrollTop(0);
});
// <A> Element event
$("a").live("click",function() { alert("clicked!"); });
无论何时单击锚点,它将从父列开始事件。任何想法?
Whenever you click the anchor it fires event from it parent row. Any ideas?
推荐答案
你必须停止事件冒泡。在jQuery中,您可以通过
You have to stop event bubbling. In jQuery you can do this by
e.stopPropagation();
在锚标签的onclick事件中。
in the onclick event of the anchor tag.
$("a").live("click",function(e){alert("clicked!");e.stopPropagation();});
编辑
Edit
看到这篇文章
jquery Event.stopPropagation()似乎不起作用
这篇关于Jquery单击事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!