我正在尝试使用 jQuery 替换给定源的 img 源。例如,当图像源为 smith.gif 时,替换为 johnson.gif。如果 williams.gif 替换为 Brown.gif 等。
编辑:图像是从 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">
etc.
那么,我该如何替换图像:IF img src="http://example.com/smith.gif" 则显示“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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)