2017-04-14 16 views
0

我想在我的代碼中包含模態。當點擊按鈕/ href時,應該在模式中顯示不同的文件。我只是從here拿到了代碼。Modal大多數情況下會立即停止

現在,加載動畫之後(或幾ms後)模式就會關閉。奇怪的是,當你按下按鈕幾次,它偶爾會工作,文件將被顯示。

這裏是我的代碼(相關部分):

// 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>

+0

所以繼承人的一點記錄的問題可能有幫助(我知道它看起來低劣^^)所以我發現,如果你按下按鈕3倍快我工作莫名其妙... https://drive.google。 com/file/d/0B4ilpgGKqcEZbzBZaHN3QlhLU1k /查看 – Niklas

回答

0

你的問題在我看來是事件的傳播。無論何時您單擊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"; 
    } 
}; 
+0

首先感謝您的努力。它看起來像在Firefox中沒有發生任何事情與你的代碼,而在鉻同樣的老問題percists。 – Niklas

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功能只需編輯我爲你把你的鏈接在「您的代碼在這裏」創建示例代碼。

+0

首先感謝您的努力。什麼代碼(什麼部分)我必須在/ *你的代碼在這裏* /?對不起,我從來沒有「工作」與js – Niklas

+0

只需放下您的鏈接 – Hacker