开发者

jQuery click on table cell event

开发者 https://www.devze.com 2023-03-11 06:45 出处:网络
I have a html code like below <tbody> <tr id=\"info\"> <td class=\"name\">Joe</td>

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.

How to do prevent 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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号