图片不会在隐藏和显示之间切换

Image will not switch between hidden and show

我无法获取图像 在 'hidden' 和 'show'

之间来回切换

我正在使用来自 How to create a hidden <img> in JavaScript?

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

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

那行代码在我的'init'函数中

如果我不注释该行,无论我点击哪个按钮,指示灯都会保持 'hidden'

我在 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

     // 

     if (document.readyState === "complete") {
       init();
     }
   });



 </script>
 </html>

visibility 样式 属性 的值为 visiblehidden

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