jQuery单击表格单元格事件

nam*_*mco 8 jquery html-table cell

我有一个像下面的HTML代码

<tbody>
  <tr id="info">
    <td class="name">Joe</td>
    <td class="surname">White</td>
    <td class="age">25</td>
 </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)

并且有一个像这样的jQuery代码:

$("tr#info").click(function() {        // function_tr
  $(this).css("background-color","yellow");
});

$("tr#info td").click(function() {     // function_td
  $(this).css("font-weight","bold");
});
Run Code Online (Sandbox Code Playgroud)

当我点击它td,function_td工作正常但function_tr也有效.
怎么做预防function_tr

Ben*_*min 12

你需要使用event.stopPropagation()

$("tr#info td").click(function(e){     //function_td
  $(this).css("font-weight","bold");
  e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)


Amj*_*sad 5

$("tr#info td").click(function(e){     //function_td
  $(this).css("font-weight","bold");
  e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/event.stopPropagation/