我认为做这样的事情会起作用,但我很困惑,为什么它不是当风格隐藏图像开始时,而是当按下按钮时没有显示它。下面的代码:如何在html中按下按钮时显示图像?
function showImg() {
x=document.getElementById("map_img")
x.style.visibility="visible";
}
<body>
<img id="map_img" src="map.jpg" style="visibility:hidden" width="400" height="400"/>
<form id="form1" name="form1" align="center">
<input type="submit" id="Map" value="Map" onclick="showImg()"/>
</form>
我也是在这两种情况下试过这样:
<input type=button id="Map" value="Map" onclick="showImg()"/>
和:
<style>
.hidden{display:none;}
.show{display:block;}
</style>
function showImg() {
x=document.getElementById("map_img")
x.class="show";
}
<body>
<img id="map_img" src="map.jpg" class="hide" width="400" height="400"/>
<form id="form1" name="form1" align="center">
<input type="submit" id="Map" value="Map" onclick="showImg()"/>
</form>
我真的失去了对如何没有这些工作,我可以帮忙吗?
啊,我不知道的功能应该是这样的,由于来自你的帮助,感谢你以及道格(:现在的工作 – user2279496
很高兴地听到,你可以! upvote道格和标记为现在回答。:) – Miro
@ user2279496功能**不应该是这样的。你拥有它的方式非常好。建议的方式只是一个选择;这里是对差异的解释:http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname。你应该使用'var'来声明你的变量'x',或者用Klor的答案来改变风格。 – Ian