简体   繁体   English

替换图像元素的 src 属性值

[英]Replacing an image element's src attribute value

I'm trying to replace the image source, but only if the image corresponds to the existing class "overview-icon--downtime".我正在尝试替换图像源,但前提是图像对应于现有类“overview-icon--downtime”。 Then there's another class, "overview-icon--degraded", which I want to replace it with (or simply putting an image address, which might prove easier).然后是另一个类,“overview-icon--degraded”,我想用它来替换它(或者简单地放置一个图像地址,这可能会更容易)。

To be replaced:将被替代:

<div class="page__overview">
<img class="page__overview-icon overview-icon--downtime" src="downtime_large.png">
</div>

To be replaced with:替换为:

<div class="page__overview">
<img class="page__overview-icon page__overview-icon--degraded" src="degraded_large.png">
</div>

I was thinking of this, but I'm not sure I'm heading in the right direction.我在想这个,但我不确定我是否朝着正确的方向前进。

document.querySelector(".page__overview-icon overview-icon--downtime").setAttribute("img", "page__overview-icon page__overview-icon--degraded");

The image loads with the page, so I'll also need to use the AJAX at the end.图像随页面一起加载,所以最后我还需要使用 AJAX。

Any ideas here please?请问这里有什么想法吗? Thanks a lot in advance!提前非常感谢! :3 :3

You are heading in the right direction.你正朝着正确的方向前进。

Attributes are the things inside of the tags.属性是标签内的东西。 In this instance, "class" and "src" are attributes of the img element.在这种情况下,“class”和“src”是 img 元素的属性。

document.querySelector will select the img element, you then need to call setAttribute('class', new class value ), and setAttribute('src', new src value ) document.querySelector 将选择 img 元素,然后您需要调用 setAttribute('class', new class value ) 和 setAttribute('src', new src value )

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

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