jQuery click on table cell event
I have a html code like below
<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:
$("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.
function_tr
?You need to use event.stopPropagation()
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});
$("tr#info td").click(function(e){ //function_td
$(this).css("font-weight","bold");
e.stopPropagation();
});
http://api.jquery.com/event.stopPropagation/
精彩评论