2011-06-09 34 views
7

我有一個像下面jQuery的點擊

<tbody> 
    <tr id="info"> 
    <td class="name">Joe</td> 
    <td class="surname">White</td> 
    <td class="age">25</td> 
</tr> 
</tbody> 

一個HTML代碼,並有一個jQuery這樣的代碼:

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

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

當我點擊tdfunction_td工作正常,但function_tr也有效。
如何防止function_tr

回答

12

您需要使用event.stopPropagation()

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