jQuery 单击表格单元格事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6290701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:38:46 来源:igfitidea点击:
jQuery click on table cell event
提问by namco
I have a html code like below
我有一个像下面这样的 html 代码
<tbody>
<tr id="info">
<td class="name">Joe</td>
<td class="surname">White</td>
<td class="age">25</td>
</tr>
</tbody>
and there is a jQuery code like this:
有一个像这样的 jQuery 代码:
$("tr#info").click(function() { // function_tr
$(this).css("background-color","yellow");
});
$("tr#info td").click(function() { // function_td
$(this).css("font-weight","bold");
});
When I click on the td
, function_td
works fine but function_tr
also works.
How to do prevent function_tr
?
当我点击 时td
,function_td
工作正常但function_tr
也有效。
怎么预防function_tr
?
回答by Benjamin
You need to use event.stopPropagation()
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});
回答by Amjad Masad
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});