开发者

jQuery Drill down selector?

开发者 https://www.devze.com 2022-12-29 07:19 出处:网络
I have made this piece of code that makes a roll hover effect on any 开发者_如何学运维image you add the class \'simplehover\' to it :

I have made this piece of code that makes a roll hover effect on any 开发者_如何学运维image you add the class 'simplehover' to it :

    //rollover effect on links 
    $(".simplehover").mouseover(function () {
        $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
    }).mouseout(function () {
        $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
    });

<img src="slogan.gif" class="simplehover" />

Im trying to get it work if you add the class 'simplehover' to the surrounding element : . (especially usefull for ASP.net asp:HyperLink tag which automatically generate the IMG tag inside the hyperlink.

<a href="#" class="simplehover"> <img src="slogan.gif" /> </a>

If I change my selector for : $(".simplehover img") , it will work, however it no longer works in senario #1.

I tried this, but no luck:

$(".simplehover").mouseover(function () {
if ($(this).has('img')) { $(this) = $(this).children(); }
...

Anyone can figure this out?

Montreal Web Design


You can refactor using a combination of the hover() method and the multiple selector.

$("a.simplehover img, img.simplehover").hover(function () {
    $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
}, function() {
    $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
});

<img src="slogan.gif" class="simplehover" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号