2014-10-20 54 views
1

的DIV當你將鼠標懸停在My文字myicon.png淡出,然後回來:如何獲得前最接近的一個

<div class="mmItemStyleChild"> 
    <img src="theImages/myicon.png" class="mIcon vertAlign mmm1" id="mMW1" /> <img src="theImages/emptyImg.png" class="mSpacerStyle" /><span id="mMW" class="vertAlign mmm">My</span> 
</div> 

$('.mmm').hover(function() { 
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': .35 }); 
}, function() { 
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': 1 }); 
}); 

我是想爲以下做同樣的,但它不是工作:

<div class="mBtnMidS mBtnMidStyle"> 
    <div> 
     <img id="mbS1" src="theImages/services_icon.png" class="mBtnIcon" /> 
    </div> 
    <div> 
     <span id="mbS" class="mbBtnText">Services</span> 
    </div> 
</div> 

$('.mbBtnText').hover(function() { 
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': .55 }); 
}, function() { 
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': 1 }); 
}); 

如何修改上面的代碼,以便它從父DIV看起來(使用類的mBtnMidS`或‘mBtnMidStyle’),並獲取影像並進行了同樣的動畫漸變作爲第一個腳本

回答

2

在你的第二個代碼塊的變化:

$(this).closest('div') 

到:

$(this).closest('div.mBtnMidS.mBtnMidStyle') 

你有它的方式,最接近DIV是跨度父DIV,但你想要一個div的父(中span的祖父母div)。

+0

謝謝。那就是訣竅。 – SearchForKnowledge 2014-10-20 16:55:08