我有一些代码,我想使用 JQuery 在我的图像文件名称的开头添加字母“mb”。这对我来说很新,所以我请求你原谅我的天真。我想要做的一个例子是将我的图像名称从“images/folder/photo.jpg”更改为“images/folder/mb_photo.jpg”。我计划在类里面使用的图像,所以我有用于选择那些已经布置好的类(class)的模型。我只是停留在如何插入图像标签的中间。
根据要求,我目前拥有的是:
$(window).resize(function(){
if ($('#content').width() < 700 ){
$('#photos_member').;
$('#photos_group').;
}
});
提前感谢你们的帮助。
最佳答案
这应该可以满足您的需求...
function updateImageSrc(add) {
if (add === true) {
$("img.photos_member:not(.src-modified), img.photos_group:not(.src-modified)")
.attr("src", function() {
var split = this.src.split("/");
split[split.length - 1] = "mb_" + split[split.length - 1];
return split.join("/");
})
.addClass("src-modified");
}
else {
$("img.photos_member.src-modified, img.photos_group.src-modified")
.attr("src", function() {
var split = this.src.split("/");
split[split.length - 1] = split[split.length - 1].substr(3);
return split.join("/");
})
.removeClass("src-modified");
}
}
要在窗口调整大小处理程序中调用它,请执行此操作...
$(window).resize(function() {
if ($("#content").width() < 700) {
updateImageSrc(true);
}
else {
updateImageSrc(false);
}
});
关于javascript - 在 Jquery 中附加在 Image 的 src 属性中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230692/