Javascript 以编程方式更改 img 标签的 src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11722400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:51:00  来源:igfitidea点击:

Programmatically change the src of an img tag

javascripthtmlimagesrc

提问by Jam Ville

How can I change the srcattribute of an imgtag using javascript?

如何使用 javascript更改标签的src属性img

<img src="../template/edit.png" name=edit-save/>

at first I have a default src which is "../template/edit.png" and I wanted to change it with "../template/save.png" onclick.

起初我有一个默认的 src,它是“../template/edit.png”,我想用“../template/save.png” onclick 来改变它。

UPDATED: here's my html onclick:

更新:这是我的 html onclick:

<a href='#' onclick='edit()'><img src="../template/edit.png" id="edit-save"/></a>

and my JS

和我的 JS

function edit()
{   
    var inputs = document.myform;
    for(var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = false;
    }
}

I've tried inserting this inside the edit(), it works but need to click the image twice

我试过在edit()中插入这个,它可以工作,但需要点击图片两次

var edit_save = document.getElementById("edit-save");
    edit_save.onclick = function(){
       this.src = "../template/save.png";
    }

采纳答案by Jam Ville

its ok now

现在好了

function edit()
{   
    var inputs = document.myform;
    for(var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = false;
    }

    var edit_save = document.getElementById("edit-save");

       edit_save.src = "../template/save.png";                              
}

回答by Matthias

Give your img tag an id, then you can

给你的img标签一个id,然后你就可以

document.getElementById("imageid").src="../template/save.png";

回答by chandanjha

You can use both jquery and javascript method: if you have two images for example:

您可以同时使用 jquery 和 javascript 方法:例如,如果您有两个图像:

<img class="image1" src="image1.jpg" alt="image">
<img class="image2" src="image2.jpg" alt="image">

1)Jquery Method->

1)Jquery方法->

$(".image2").attr("src","image1.jpg");

2)Javascript Method->

2)Javascript方法->

var image = document.getElementsByClassName("image2");
image.src = "image1.jpg"

For this type of issue jquery is the simple one to use.

对于这种类型的问题,jquery 使用起来很简单。

回答by Alessandro Pirovano

if you use the JQuery library use this instruction:

如果您使用 JQuery 库,请使用以下说明:

$("#imageID").attr('src', 'srcImage.jpg');

回答by Donatas Olsevi?ius

<img src="../template/edit.png" name="edit-save" onclick="this.src = '../template/save.png'" />

回答by Marcelo Camargo

In this case, as you want to change the srcof the first value of your element, you have no need to build up a function. You can change this right in the element:

在这种情况下,由于您想更改src元素的第一个值,因此无需构建函数。您可以在元素中更改此权限:

<a href='#' onclick='this.firstChild.src="../template/save.png"')'>
  <img src="../template/edit.png" id="edit-save"/>
</a>

You have several ways to do this. You can also create a function to automatize the process:

您有几种方法可以做到这一点。您还可以创建一个函数来自动化该过程:

function changeSrc(p, t) { /* where p: Parent, t: ToSource */
  p.firstChild.src = t
}

Then you can:

然后你可以:

<a href='#' onclick='changeSrc(this, "../template/save.png");'>
  <img src="../template/edit.png" id="edit-save"/>
</a>

回答by Fabrizio Calderan

With the snippet you provided (and without making assumptions about the parents of the element) you could get a reference to the image with

使用您提供的代码段(并且不假设元素的父元素),您可以获得对图像的引用

document.querySelector('img[name="edit-save"]');

and change the src with

并更改 src

document.querySelector('img[name="edit-save"]').src = "..."

so you could achieve the desired effect with

所以你可以达到预期的效果

var img = document.querySelector('img[name="edit-save"]');
img.onclick = function() {
    this.src = "..." // this is the reference to the image itself
};

otherwise, as other suggested, if you're in control of the code, it's better to assign an idto the image a get a reference with getElementById(since it's the fastest method to retrieve an element)

否则,正如其他人建议的那样,如果您控制代码,最好为id图像分配一个获取引用的引用getElementById(因为它是检索元素的最快方法)

回答by Ben

Give your image an id. Then you can do this in your javascript.

给你的图片一个id。然后你可以在你的javascript中做到这一点。

document.getElementById("blaah").src="blaah";

You can use the ".___" method to change the value of any attribute of any element.

您可以使用“.___”方法来更改任何元素的任何属性的值。

回答by Po Po

Maybe because you have a tag like a parent of the tag. That why you have to click two time the images.

也许是因为你有一个标签,比如标签的父级。这就是为什么您必须单击两次图像的原因。

For me the solution is this: http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb

对我来说,解决方案是这样的:http: //www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb