Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

javascript - 以编程方式更改img标记的src(Programmatically change the src of an img tag)

How can I change the src attribute of an img tag using javascript?(如何使用javascript更改img标签的src属性?)

<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"; }   ask by Jam Ville translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...