我的javascript:
$(".openPage").click(function() {
alert(1);
});
$(".openButton").click(function() {
alert(2);
});
我的 html:
<tr class='openPage'>
<td></td>
<td><button class='openButton'></button></td>
</tr>
当我在 td 中单击我的按钮时,将触发第一个函数而不是带有警报 (2) 的函数。我该如何解决这个问题?
最佳答案
您只需在 button
项上执行 event.stopPropagation()
。就这样
$(".openPage").click(function() {
alert(1);
});
$(".openButton").click(function(evt) {
evt.stopPropagation(); // The click won't propagate to .openPage parent
alert(2);
});
关于javascript - 使用 .click 在 tr 和 td 上使用不同的类 2 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333848/