【问题标题】:onclick event don't work on WEB pageonclick 事件在 WEB 页面上不起作用
【发布时间】:2018-01-16 14:33:32
【问题描述】:

我想创建一个模态框。它应该在用户单击按钮时出现,并在用户单击模式框中的关闭按钮时关闭。 我做了两个函数:

  • check() : 它改变了 CSS。在它的elememt onclick之后,出现模态框
  • close() : 这个函数应该关闭模态框,但它没有。它只是应该设置以前的 css 设置。当我单击关闭按钮(span 元素)时,此功能根本不执行。

为什么关闭功能不起作用,我应该对什么进行现代化改造以获得结果?

提前致谢)

function check() {
    var modal = document.getElementById("myModal");
    modal.style.display = "block";
}
//this function dont't work
function close(){
    var modal = document.getElementById("myModal");
    modal.style.display = "none";
}
.modal {
    display: none;
    position: fixed;
    z-index: 1;
    padding-top: 100px;
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%;
    overflow: auto;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0,0.4);
}


.modal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}
<html>
<body>

  <button id="button" type="submit" onclick="check()">Login</button>
  <div id="myModal" class="modal">
    <div class="modal-content" >
      <span class="close" onclick="close()">&times;</span>
      <p >Some text in the Modal..</p>
    </div>
  </div>


</body>
</html>

【问题讨论】:

    标签: javascript css html user-interface


    【解决方案1】:

    close 已经是本机浏览器函数的名称,window.close()。重命名你的函数,它工作正常:

    function check() {
        var modal = document.getElementById("myModal");
        modal.style.display = "block";
    }
    
    function closeModal(){
        var modal = document.getElementById("myModal");
        modal.style.display = "none";
    }
    .modal {
        display: none;
        position: fixed;
        z-index: 1;
        padding-top: 100px;
        left: 0;
        top: 0;
        width: 100%; 
        height: 100%;
        overflow: auto;
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0,0.4);
    }
    
    
    .modal-content {
        background-color: #fefefe;
        margin: auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
    }
    <html>
    <body>
    
      <button id="button" type="submit" onclick="check()">Login</button>
      <div id="myModal" class="modal">
        <div class="modal-content" >
          <span class="close" onclick="closeModal()">&times;</span>
          <p >Some text in the Modal..</p>
        </div>
      </div>
    
    
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多