我有一个从另一个 php 页面生成的图像
<img style="margin:9px 0 0 16px;" src="sample.php" width="87" height="59" id="imagid">
每次都会创建验证码图像。每次刷新时都会生成不同的图像。 但我想要的是在那里放一个按钮来更改图像而不刷新整个页面。
<input type="button" value="refresh" onclick="refresh();">
我的js代码是
function refresh(){
document.getElementById("imagid").src="sample.php";
}
不行,如何通过js改变图片src
最佳答案
在您的 JS 代码中,您尝试更改按钮的“src”属性,您需要将 id 应用于图像,而不是按钮。
您需要做的是满足以下条件:
<img style="margin:9px 0 0 16px;" src="sample.php" width="87" height="59" id="imagid">
<input type="button" value="refresh" id="imagid" onclick="refresh();">
<script>
function refresh() {
document.getElementById("imagid").src = "sample2.php";
}
</script>
关于javascript - 通过js从php页面更改图像的src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22191460/