2011-08-11 12 views
1

我有6個TDS是這樣的:如何添加類到下一個鏈接懸停在jQuery的td?

<td><a href="#">link</a></td> 
<td>Some text here</td> 

<td><a href="#">link</a></td> 
<td>Some other text here</td> 

<td><a href="#">link</a></td> 
<td>Some other other text here</td> 

而且我想在鏈接到下一個TD懸停後addClass。例如。如果我將鼠標懸停在第二個TD的鏈接在接下來的TD將有例如一類活躍這樣

<td><a href="#">link</a></td> 
<td>Some text here</td> 

<td><a href="#">link</a></td> <!-- hoovering over this link --> 
<td class="active">Some other text here</td> <!-- and this td will have class active--> 

<td><a href="#">link</a></td> 
<td>Some other other text here</td> 

如何做到這一點?

回答

2
$('a').hover(function(){ 
    $(this).parent().next().addClass('someclass'); 
}, function(){ 
    $(this).parent().next().removeClass('someclass'); 
}); 
1
jQuery(function($){ 
    $("a").hover(function(){ 
     $(this).parent().next("td").addClass("active"); 
    }); 
}); 

這裏是fiddle

1

這不僅會增加類懸停但是當你徘徊了一個鏈接也將其刪除。當您的網頁上有其他標籤時,它也可以使用。不僅在td內。

$(document).ready(function() { 
    $("td > a").hover(function() { 
     $(this).parent().next("td").toggleClass("active"); 
    }); 
}); 
相關問題