-2

我想用 JavaScript 显示和隐藏一个 div。有人有额外的代码吗?到目前为止,这是我的代码:

 <script>
 window.onload=function(){$hide.div(div)};
 </script>

HTML:

 <div class="overlay"></div>
 <div class="modal">I'm the modal window!</div>

CSS:

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-color: rgba(0,0,0,0.5);
    z-index: 10;
}

.modal {
    width: 300px;
    height: 200px;
    line-height: 200px;
    position: fixed;
    top: 50%; 
    left: 50%;
    margin-top: -100px;
    margin-left: -150px;
    background-color: #f1c40f;
    border-radius: 5px;
    text-align: center;
    z-index: 11;
}
4

2 回答 2

2

使用 javascript:

<script>
    window.onload=function(){  
      for(var i = 0;i < document.getElementsByTagName("div").length;i++) {
        document.getElementsByTagName("div")[i].style.visibility  = "hidden";
      } 
     };
</script>

我推荐你使用jQuery

<script>
 $(document).ready(function() {
    $("div").hide();
 });
</script>
于 2013-11-11T09:17:51.270 回答
2

display只需为此使用 css属性:

document.getElementsByClassName('modal')[0].style.display = 'none';

并再次展示它:

document.getElementsByClassName('modal')[0].style.display = 'block';

jsFiddle

于 2013-11-11T09:21:42.927 回答