gpt4 book ai didi

javascript - 图像不会在隐藏和显示之间切换

转载 作者:行者123 更新时间:2023-11-27 22:52:28 26 4
gpt4 key购买 nike

我无法让图像在“隐藏”和“显示”之间来回切换

我使用的想法来自 How to create a hidden <img> in JavaScript?

我有两个不同的按钮,尝试一个使用 html,另一个使用 javascript - 如果我注释掉一行,则会显示灯泡照片

 //document.getElementById("light").style.visibility = "hidden";

该行代码位于我的'init'函数

如果我不注释该行,无论我单击哪个按钮,指示灯都会保持“隐藏”状态

我在 Safari 中的 Web 控制台登录中没有看到任何错误

 <!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>

最佳答案

visibility style 属性的值为 visiblehidden

没有显示值。

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";
}

关于javascript - 图像不会在隐藏和显示之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com