2011-09-15 56 views
0

我有一个菜单,当一个项目被点击时,我想将图标更改为不同的颜色。我通过改变点击的图像的src来实现这个工作! 我的改变src图像/银/的icon.png到图像/蓝/的icon.png在ajax菜单中更改图标?

如果我点击一个项目(与银色图标),那么它的颜色变为蓝色(良好),但是当我点击另一个项目的第一个项目仍然是蓝色的,所以点击一下后,所有项目都是蓝色的。

所以我需要的帮助是如何获得所有图像来源,但不是点击,并将其更改为银?或者如果有另一种解决方案?谢谢!

我用这个代码:karim79 我测试了它,但现在所有图标留银,他们不改变在所有

$ (function() { 
$("#menu > li > a").click(function() { 

//gets the image source 
var menyitem = $(this).children("img").attr("src"); 


// searce and replace "silver" with "blue" 
var changedSrc=menyitem.replace("silver", "blue"); 


// changes the attribute SRC with the new one 
$(this).children("img").attr("src", changedSrc); 


}); 
}); 

回答

0
$(function() { 
    $("#menu > li > a").click(function() { 

     // reset all images 
     $("#menu > li > a > img").attr("src", function(i, val) { 
      return val.replace("blue", "silver"); 
     }); 

     //gets the image source 
     var menyitem = $(this).children("img").attr("src"); 


     // searce and replace "silver" with "blue" 
     var changedSrc = menyitem.replace("silver", "blue"); 


     // changes the attribute SRC with the new one 
     $(this).children("img").attr("src", changedSrc); 


    }); 
}); 
+0

感谢。 –

+0

我想它会取代所有图像来源,甚至是我点击的图像来源。它需要替换除了我点击的那个之外的所有内容吗? –

相关问题