显示隐藏使用样式不起作用

时间:2018-04-23 16:56:27

标签: javascript html

有人可以向我解释为什么这个简单的网页不会做它应该做的事情,也就是说,将样式从display:none;更改为display:block; ??

谢谢!

function ShowHideModal(elemID) {
    var Selem = document.getElementById(elemID);
    if (Selem != null)
    {
        if (Selem.style.display == "none")
            Selem.style.display = "block;"
        else
            Selem.style.display = "none;"
    }
}
<a href="javascript: ShowHideModal('test1');">TryIt</a>
<div id="test1" style="display:none;">
    This is my content baby!
</div>

1 个答案:

答案 0 :(得分:4)

您正在放置&#34; ;&#34;在&#34; block;&#34;的引文内和&#34; none;&#34;。

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title></title>

</head>

<body>

  <a href="JavaScript: ShowHideModal('test1');">TryIt</a>
  <div id="test1" style="display:none;">
    This is my content baby!
  </div>

  <script type="text/javascript">
    function ShowHideModal(elemID) {
      var Selem = document.getElementById(elemID);

      if (Selem != null) {
        if (Selem.style.display === "none") {
          Selem.style.display = "block";
        } else {
          Selem.style.display = "none";
        }
      }
    }
  </script>

</body>

</html>
&#13;
&#13;
&#13;