2013-11-01 53 views
1

試圖選擇一個$內的元素(這)選擇

我有一個類.category_row_belts的幾個div的。在每一個內部都有一個封裝了H2標籤和圖像的錨標籤。

當在div上滾動時,我試圖使用jQuery的.hover()和addClass()同時在H2和img上創建翻轉效果。換句話說,當在div上滾動時,所有元素都點亮。顯然我必須使用$(this)並嘗試使用children(),但它不起作用。

<div class="category_row_belts"> 
<a href="linkhere.html"> 
<h2 class="belts-cat-description" >Product name here</h2> 
<img src="img/product-pic.jpg"> 
</a> 
</div> 

我的JS到目前爲止...

$('.category_row_belts').hover(
function(){ 
    $(this).children('a > img').addClass('rolloverborder'); 
    $(this).children('a > h2').removeClass('belts-cat-description'); 
    $(this).children('a > h2').addClass('rollovertxt'); 
}, 
function(){ 
    $(this).children('a > img').removeClass('rolloverborder'); 
    $(this).children('a > h2').removeClass('rollovertxt'); 
    $(this).children('a > h2').addClass('belts-cat-description'); 
}) 
+0

好象喜歡的事,可以用CSS來實現 - >'.category_row_belts:懸停h2.belts-CAT-描述{風格}' – adeneo

+0

哦,並嘗試用find()替換children()。 – adeneo

回答

2
$('.category_row_belts').hover(
function(){ 
    $(this).find('img').addClass('rolloverborder'); 
    $(this).find('h2').removeClass('belts-cat-description') 
    .addClass('rollovertxt'); 
}, 
function(){ 
    $(this).find('img').removeClass('rolloverborder'); 
    $(this).find('h2').removeClass('rollovertxt') 
    .addClass('belts-cat-description'); 
}) 
+0

好的,謝謝,我會嘗試 – TopTomato

+0

工作,謝謝! – TopTomato