我想用jQuery替换给定源的img源。例如,当图像src是smith.gif时,请替换为johnson.gif。如果williams.gif替换为brown.gif等用jQuery动态替换img src属性
编辑:图像从XML检索到随机顺序,没有类到每个。
这是我的尝试:
if ($("img").attr('src', 'http://example.com/smith.gif')) {
$(this).attr('src', 'http://example.com/johnson.gif');
}
if ($("img").attr('src', 'http://example.com/williams.gif')) {
$(this).attr('src', 'http://example.com/brown.gif');
}
请注意,我的HTML有许多图像。例如
<img src="http://example.com/smith.gif">
<img src="http://example.com/williams.gif">
<img src="http://example.com/chris.gif">
等
所以,我怎么能更换图片:IF IMG SRC = “http://example.com/smith.gif” 然后显示“HTTP://例子.COM/williams.gif”。等等
非常感谢
那么,什么是你的问题? – Jon
你错过了在你的HTML例子中关闭''' –
你正在使用'$(img)'来检查属性,'$(this)'来设置它。这真的是你想要做的吗? ?' –