jquery翻转问题

时间:2011-01-16 18:32:57

标签: jquery

我做了一个图像翻转(第一个我用教程制作)我想知道是否有办法杀死脚本如果一个图像以over.jpg结束并使脚本继续如果图像没有'吨

这是我的代码

$('#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>

我尝试添加

if($('img[src$=.gif]')
在脚本之前

但它不起作用......

3 个答案:

答案 0 :(得分:2)

这个怎么样:

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

请参阅http://api.jquery.com/not/

答案 1 :(得分:2)

您可以使用not()方法,如下所示:

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

答案 2 :(得分:0)

你的意思是,你需要这样的东西吗?


$('#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');
           });
   }
}

您可以在此处使用选择器,我测试了代码:http://jsfiddle.net/Exceeder/NE3Ps/