简体   繁体   English

使用javascript仅更改类的所有img src中的一个字符

[英]Change only one character of all img src of a class using javascript

I want to change the src of all the images which are in the 'car-image' class. 我想更改“ car-image”类所有图像的src

But I do not have to change whole url. 但我不必更改整个网址。 I just want to change one character . 我只想换一个字符

I want edit this - 我要编辑-

<div class="car-image">
    <img src="/cars/3_large_1.png">
</div>

To this- 为此

<div class="car-image">
    <img src="/cars/3_large_2.png">
</div>

And this format is common in all the image in this class. 这种格式在此类中的所有图像中都很常见。 I tried something like this- 我尝试过这样的事情-

var allsrc = document.getElementsByClassName('car-image');
allsrc[0].src="/cars/3_large_2.png";

This is not working. 这是行不通的。 How can i do this in javascript? 如何在javascript中做到这一点?

you are setting src of wrong node allsrc returns your div not the image. 您正在设置错误节点的src allsrc返回div而不是图像。

Try this 尝试这个

allsrc[0].childNodes[1].setAttribute("src","/cars/3_large_2.png")

I want to change the src of all the images which are in the 'car-image' class using javascript. 我想使用javascript更改“ car-image”类中所有图像的src。

You can change <img> src for all car-image classes like this: 您可以为所有car-image类更改<img> src ,如下所示:

 var all = document.getElementsByClassName('car-image'); for(var i = 0; i < all.length; i++){ var image = document.getElementsByClassName('car-image')[i].getElementsByTagName('img'); image[0].setAttribute("src", "/cars/3_large_2.png"); } 
 <div class="car-image"> <img src="/cars/3_large_1.png"> </div> <div class="car-image"> <img src="/cars/5_large_1.png"> </div> <div class="car-image"> <img src="/cars/7_large_1.png"> </div> <div class="car-image"> <img src="/cars/9_large_1.png"> </div> 

(Inspect elements and see new src's) (检查元素并查看新的src)

A more elegant solution would be to use replace function with regex. 一个更优雅的解决方案是将替换功能与正则表达式一起使用。 If you know the image src pattern and similar changes apply to all image src, you can build a regex. 如果您知道映像src模式并且类似的更改适用于所有映像src,则可以构建正则表达式。 In that case, instead of changing each image src one by one, you can iterate over the elements that contains car-image class and find out the first childNode and change the src attr. 在这种情况下,您可以遍历包含car-image类的元素并找出第一个childNode并更改src属性,而不是一个一个地更改每个图像src。

// find all elements that contains class car-image
var carImgDivs = document.getElementsByClassName('car-image');

// iterate over carImgDivs and execute an imediate function to just pass the
// childNode1 that is the image. Use replace function with regex to find out the
// changed image src value and set the changed src value to childNode1
for(var i = 0; i < carImgDivs.length; i++) (function(childNode1) {
  if(childNode1) { 
    var replacedSrc = childNode1.getAttribute('src').replace(/(_)(\d)/, "$12");
    childNode1.setAttribute("src", replacedSrc);
  }
})(carImgDivs[i].childNodes[1]);

For a image src like /cars/3_large_1.png , the regular expression (_)(\\d) matches a underscore that follows a digit and captures both. 对于像/cars/3_large_1.png这样的图像src,正则表达式(_)(\\ d)匹配一个跟在数字后面的下划线并同时捕获两者。 The $1 in replace string "$12" says to keep the first capture group(underscore) as it is and 2 says to replace the second capture group(a digit) with 2 . 替换字符串"$12"$1表示将第一个捕获组(下划线)保持原样,而2表示将第二个捕获组(一个数字)替换为2 Basically, the regex matches with _1 in the above image src. 基本上,正则表达式与上图src中的_1匹配。 _ is the first capture group and 1 is the second capture group. _是第一个捕获组, 1是第二个捕获组。 So, in the end, the image src gets changed to /cars/3_large_2.png 因此,最后,图像src更改为/cars/3_large_2.png

Use(jQuery solution) : $( "img:nth-child(1)" ).attr('src', <new_name>); 使用(jQuery解决方案): $( "img:nth-child(1)" ).attr('src', <new_name>);

The nth-child(i) means i th image. nth-child(i)表示第i个图像。

Example: 例:

$(".car_image img:nth-child(1)").attr('src', '/cars/3_large_2.png');

To change all the images just remove the :nth-child() 要更改所有图像,只需删除:nth-child()

如果您在页面中包含jQuery,则可以执行

$(".car-image img").attr("src", "/cars/3_large_2.png");

What about this 那这个呢

var x = document.getElementsByClassName('car-image')[0]; 
var img = x.getElementsByTagName('img')[0];
img.src = "/cars/3_large_2.png";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM