我想要一个显示和隐藏我的图像的按钮

时间:2015-10-18 06:12:49

标签: javascript image show-hide

<!DOCTYPE html>
<html>
<head>
<title>Face</title>
<meta charset="UTF-8">
<script type="text/javascript">
//<![CDATA[

function myFunction(id) {
       var e = document.getElementById(id);
       if(e.style.visibility == "visible")
         e.style.visibility = 'hidden';
       else
          e.style.visibility = 'visibile';
    }



</script>
</head>
<body>

<div style="position: relative; visibility: visible;">
<img src="http://vignette4.wikia.nocookie.net/mrmen/images/5/52/Small.gif/revision/latest?cb=20100731114437"
          alt="Pumpkins" id="Pum"/>
<button onclick="myFunction('Pum')">Face</button>
</div>






</body>
</html>

我想要一个显示/隐藏我的图像的按钮。我不明白我做错了什么。我收到一个错误,上面写着“TypeError:无法读取null的属性'可见性'”。如何修复错误并使我的程序正常工作?

1 个答案:

答案 0 :(得分:0)

您的问题是代码中的拼写错误

function myFunction(id) {
       var e = document.getElementById(id);
       if(e.style.visibility == "visible")
         e.style.visibility = 'hidden';
       else
          e.style.visibility = 'visible'; // <-------- SHOULD BE 'visible', was 'visibile'
    }

这可以解决您的问题。