简体   繁体   中英

jquery rollover question

i made an image roll over (first one i make with out tutorials) and i want to know if there is a way to kill the script if the one image ends with over.jpg and make script continue if the image doesn't

this is my code

$('#topNav a img').hover(function(){
    var rSrc = $(this).attr('name');
    $(this).attr('src','images/' + rSrc + 'over.jpg');
}, function(){
    var rSrc = $(this).attr('name');
    $(this).attr('src', 'images/' + rSrc + '.gif');
});

html

    <a href="index.html" class="nav"><img src="images/m_1over.jpg" name="m_1"/></a>
<a href="aboutus.html" class="nav"><img src="images/m_2.gif" name="m_2"/></a>

i tried adding

if($('img[src$=.gif]')

before the script but it doesn't work...

How about this:

$('#topNav a img').not("[src$=over.jpg]").hover(function(){ ...

See http://api.jquery.com/not/

您可以像这样使用not()方法:

$('#topNav a img').not('#topNav a img[src=over.jpg]').hover(function(){...}

You mean, you need something like this?


$('#topNav a img').each(function(i, el) {
   if ($(el).attr('src').match('\\.gif$') == '.gif') 
   {
           $(el).hover(function(){
              var rSrc = $(this).attr('name');
              $(this).attr('src','images/' + rSrc + 'over.jpg');
           }, 
           function(){
              var rSrc = $(this).attr('name');
              $(this).attr('src', 'images/' + rSrc + '.gif');
           });
   }
}

You can play with the selector here, where I tested the code: http://jsfiddle.net/Exceeder/NE3Ps/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM