用jQuery动态替换img src属性 [英] Dynamically replace img src attribute with jQuery
本文介绍了用jQuery动态替换img src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery替换给定源的img源代码。例如,当图像src是smith.gif时,请替换为johnson.gif。如果williams.gif替换为brown.gif等。
I am trying to replace the img source of a given source using jQuery. For example, when the image src is smith.gif, replace to johnson.gif. If williams.gif replace to brown.gif etc.
编辑:图像从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有很多图片。例如
Note that my HTML has many images. For example
<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:/ /example.com/williams.gif。等......
So, how can I replace the images: IF img src="http://example.com/smith.gif" then show "http://example.com/williams.gif". etc...
非常感谢
推荐答案
这就是你想做的事情:
var oldSrc = 'http://example.com/smith.gif';
var newSrc = 'http://example.com/johnson.gif';
$('img[src="' + oldSrc + '"]').attr('src', newSrc);
这篇关于用jQuery动态替换img src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文