我无法获取图片在隐藏的'之间来回切换和'显示'
我正在使用来自的想法 How to create a hidden <img> in JavaScript?
我有两个不同的按钮,使用html尝试一个,另一个使用javascript - 如果我注释掉一行,则显示灯泡照片
//document.getElementById("light").style.visibility = "hidden";
这行代码在我的&#39; init&#39;功能
如果我不对该线进行评论,则无论我点击哪个按钮,灯都会保持隐藏状态
我在Safari控制台日志中看不到任何错误
<!DOCTYPE html>
<html>
<body>
<h1>Switch on the Light</h1>
<img id="light" src="WebVuCoolOldBulb-2.jpg" style="width:100px" >
<button type = button
onclick="document.getElementById('light').src.show ='WebVuCoolOldBulb-2.jpg'" >Switch On the Light
</button>
<input type="button" id="onButton" value="ON" />
</body>
<script>
//document.images['light'].style.visibility = hidden;
function init() {
//document.getElementById("light").style.visibility = "hidden";
var onButton = document.getElementById("onButton");
onButton.onclick = function() {
demoVisibility() ;
}
}
function demoVisibility() {
document.getElementById("light").style.visibility = "show";
}
document.addEventListener('readystatechange', function() {
// Seems like a GOOD PRACTICE - keeps me from getting type error I was getting
// https://stackoverflow.com/questions/14207922/javascript-error-null-is-not-an-object
if (document.readyState === "complete") {
init();
}
});
</script>
</html>
答案 0 :(得分:1)
visibility
样式属性的值为visible
和hidden
。
没有show
值。
function init() {
document.getElementById("light").style.visibility = "hidden";
var onButton = document.getElementById("onButton");
onButton.onclick = function() {
demoVisibility();
}
}
function demoVisibility() {
document.getElementById("light").style.visibility = "visible";
}