javascript - Modal moSTLy 保持立即退出

标签 javascript css file dynamic modal-dialog

我想在我的代码中包含一个模式。单击按钮/href 时,模式中应显示不同的文件。我只是从 here 中获取了代码.

现在,在加载动画后(或几毫秒后)模态窗口就关闭了。奇怪的是,当你按下按钮几次时,它偶尔会起作用并显示文件。

这是我的代码(相关部分):

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
    modal.style.display = "flex";
};

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
};

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
};
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}

/* Modal Header */
.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

/* Modal Footer */
.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
<div class="nav1">

        <a href="index.php?page=users&action=login" id="myBtn">Login</a>
        
</div>

    <div id="myModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <span class="close">&times;</span>
                <h2>Modal Header</h2>
            </div>
            <div class="modal-body">
                Modal Body
    <?php
    if (isset($_GET["page"]) ) {
        switch ($_GET["page"]) {
            case "post":
                include "./system/post/index.php";
                break;
            case "users":
                include "./system/users/index.php";
                break;
            default:
                include "start.php";
                break;
        }
    }
    else
    {
        include "start.php";
    }
    ?>

            </div>
            <div class="modal-footer">
                Modal Footer
            </div>
        </div>
        </div>
    <script src="./system/style/modal.js"></script>

最佳答案

在我看来,您的问题是事件的传播。每当您单击 btnspan 时,模态窗口都不会显示,因为最终 window.onclick 发生并且模态窗口关闭。因此,解决方案是在 btn/span 点击事件中添加 event.stopPropagation():

btn.onclick = function(event) {
  event.stopPropagation(); // stop the event to bubble up
  modal.style.display = "flex";
};
span.onclick = function(event) {
  event.stopPropagation(); // stop the event to bubble up
  modal.style.display = "none";
};
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
};

关于javascript - Modal moSTLy 保持立即退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409910/

相关文章:

javascript - D3.JS SVG 圆圈中的数据驱动颜色

javascript - 实现 ajax 时遇到问题。 if 语句不起作用

javascript - 两个淡入淡出切换效果之间的 Jquery 延迟

javascript - Dojo - 将 dijit 添加到关闭的 TitlePane

css - 使用相对长度垂直对齐图像和输入字段

html - 水平和垂直居中 HTML 对象

css - Bootstrap 简单页面在移动设备上没有响应

java - Java中转到文件行号

Java:根据行首从文件中读取行

Python - 一个干净的方法来解决这个问题?