2014-10-20 22 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