2011-06-13 56 views
0

這裏是我的HTML:如何在TR中解除綁定jQuery中的TD的鼠標懸停?

<table id="myTable"> 
    <tr class="myTr"> 
     <td> 
      CELL 1 
     </td> 
     <td> 
      Cell 2 
     </td> 
     <td class="notThis"> 
      Cell 3 
     </td> 
    </tr> 
    <tr class="myTr"> 
     <td> 
      2- CELL 1 
     </td> 
     <td> 
      2- Cell 2 
     </td> 
     <td> 
      2- Cell 3 
     </td> 
    </tr> 
</table> 

<div id="myDiv">CONSOLE</div> 

而且我的javascript:

$(document).ready(function() { 
    $(".myTr").mouseover(function() { 
     $("#myDiv").html("OVER"); 
    }); 
}); 

我希望它這樣,當你將鼠標放置在 「notThis」 電池,鼠標懸停不會觸發。我有一個小提琴設置來進行測試:http://jsfiddle.net/S7bfH/3/

感謝

+0

如果綁定到td元素的處理程序與event.stopPropagation()做到這一點你正在改變mouseover事件的CSS,調整t可能會更容易他CSS的td.notThis顯示不變。 – 2011-06-13 16:00:53

回答

3

這裏:

WORKING DEMO

$(".myTr td:not('.notThis')").hover(function() { 
     $("#myDiv").html("ACTIVE"); 
    }, function() { 
     $("#myDiv").html("INACTIVE");   
    }); 
2

你需要停止事件傳播;這可以防止事件冒泡到其他處理程序綁定的祖先元素。

$('.notThis').mouseover(function(event) { 
    event.stopPropagation(); 
}); 
-2

使用。不能()方法,例如

$(".myTr").not(".notThis").mouseover(function() { 
+1

這沒有什麼幫助,因爲事件從來沒有被綁定到'.notThis'上。這只是將處理程序綁定到不是'.notThis'的所有'.myTr'元素 - 這都是它們!問題在於事件冒泡,在這裏。 – lonesomeday 2011-06-13 16:04:27