我有一个地狱 CMS 可以使用,我想知道我是否可以使用 jQuery(或直接使用 JS)执行以下操作
显示的图像没有 ID 或类,我想根据它们的 src 值替换图像
伪代码将类似于...
在 src = /img/00789-reg-1.jpg' 处找到 img;并将 src 值替换为 ='img/much-better-img.jpg';
谢谢 -
我有一个地狱 CMS 可以使用,我想知道我是否可以使用 jQuery(或直接使用 JS)执行以下操作
显示的图像没有 ID 或类,我想根据它们的 src 值替换图像
伪代码将类似于...
在 src = /img/00789-reg-1.jpg' 处找到 img;并将 src 值替换为 ='img/much-better-img.jpg';
谢谢 -
每个图像都是由 提供的数组的一部分document.images
。您可以循环浏览图像以找到具有所需来源的图像。
var index=0;
while(document.images[index]){
alert(document.images[index].src);
index++;
}
是的你可以:
jQuery('img[src=/img/00789-reg-1.jpg]').attr('src','img/much-better-img.jpg');
你能用:
var images = document.images;
var iLength = images.length;
for (var i=0; i<iLength; i++){
thisImage = images[i];
if (thisImage.src == "theOneThatIWantToReplace"){
thisImage.src = "myNewSrc"
break;
}
}
如果您不需要它,至少这不会涉及到使用 JQuery。
var replace = {
"/img/00789-reg-1.jpg": "img/much-better-img.jpg",
"/img/another.jpg": "img/another-img.jpg"
}
for (var src in replace){
$("img[src="+src+"]").attr("src", replace[src]);
}
您可以使用Attribute Starts With Selector来查找您的图像,然后进行替换:
$("img[src^='/img']").each(function(){
$(this).attr("src", "new image");
});
您可以获取所有图像,然后检查它的来源
$('img').foreach(function(o){
if (o.attr('src')=='oldfile')
o.attr('src', 'newfile');
});
这很长,但我不知道其他方式。