[英]How can i swap one term in img src on all dom images using javascript / JQuery?
I need to swap all img src /products/210x110/foo-bar-foo-front.jpg to /products/210x110/bar-foo-bar-angle.jpg & apply it to every image in the dom. 我需要将所有img src /products/210x110/foo-bar-foo-front.jpg交换到/products/210x110/bar-foo-bar-angle.jpg并将其应用到dom中的每个图像。
My current code works but applies the src url from the first image in the dom to all img src attributes: 我当前的代码可以工作但是将drc中第一个图像的src url应用到所有img src属性:
i need to just replace 1 word... 我需要替换1个字......
//toggle image angles
var img = $("div.product-image a img");
$('div#image-angle-switch').toggle(function() {
$(this).html('<span></span> Angle view ')
switch_to_low_fi();
}, function() {
$(this).html('<span></span> Front view')
img.attr("src", img.attr("src").replace("front", "angle"));
});
$('div#image-angle-switch').click(function() {
img.attr("src", img.attr("src").replace("toggle"));
});
Could i create an array of each img src, swap one word and reapply to every img? 我可以创建每个img src的数组,交换一个单词并重新应用到每个img吗?
var tn_array = $("div.product-image a img").map(function() {
return $(this).attr("src");
});
for (var i=0; i<tn_array.length; i++) {
console.log(tn_array[i]);
}
Got my array but how should i apply it? 得到了我的阵列,但我该如何应用呢? Any help much appreciated.
任何帮助非常感谢。
Loop over each image and change it's src. 循环遍历每个图像并更改它的src。
$('#image-angle-switch').data("view", "front").click(function () {
var $this = $(this);
if ($this.data("view") == "front") {
$this.html('<span></span> Angle view ').data("view","angle");
// this does the looping
$("div.product-image a img").attr("src",function (i, src) {
return src.replace("front", "angle");
});
} else {
$this.html('<span></span> Front view ').data("view", "front");
// this does the looping
$("div.product-image a img").attr("src", function (i, src) {
return src.replace("angle", "front");
});
}
});
Also replaced the old depreciated toggle method. 还替换了旧的折旧切换方法。 Could be made dryer though
虽然可以做干燥机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.