Modal主要是立即戒烟

时间:2017-04-14 10:40:38

标签: 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>

2 个答案:

答案 0 :(得分:0)

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

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";
  }
};

答案 1 :(得分:0)

// 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 = "Block";
   $('a').click( function(e) {e.preventDefault(); /*your_code_here;*/ return false; } );

};

// 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";
    }
};

只需在btn.click函数中编辑我已创建示例代码,您可以将链接放在“此处的年轻代码”中。