2016-10-31 3 views
0

複数のモーダルを作成して異なるデータを表示しようとしています。 Iveは他の例(スタックオーバーフロー)を見て、コードを実装しましたが、まだ私のために働いていません。基本的にこの時点で、私はそれを働かせて、WW3 Schoolsのウェブページからスクリプトをコピーしています。私は2つの例を持っています... 1つのモーダル(動作する)と2つのモーダル(異なるデータを表示する必要があります)は最初のモーダルを削除すると2つ目のモーダルが動作します。その別の問題、アイブ氏はまた、エラーが存在しないと言っ要素とそのを視察した。私はあなたが私を助けることができることを願っています念のためにページ全体を含める!複数のモーダルを作成して異なるデータを表示するにはどうすればよいですか?

最初の例は、1つのモーダル(作品)で。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Modals</title> 
<style> 
/* The Modal (background) */ 
.modal { 
    display: none; /* Hidden by default */ 
    position: fixed; /* Stay in place */ 
    z-index: 1; /* Sit on top */ 
    padding-top: 100px; /* Location of the box */ 
    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 */ 
.modal-content { 
    background-color: #fefefe; 
    margin: auto; 
    padding: 20px; 
    border: 1px solid #888; 
    width: 80%; 
} 

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

.close:hover, 
.close:focus { 
    color: #000; 
    text-decoration: none; 
    cursor: pointer; 
} 
</style> 
</head> 
<body> 
<h2>Modal Example</h2> 

    <!-- Trigger/Open The Modal --> 
    <button id="myBtn" type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal</button> 

    <!-- The Modal --> 
    <div id="myModal1" class="modal"> 

     <!-- Modal content --> 
     <div class="modal-content"> 
     <span class="close">×</span> 
     <p>Some text in the Modal..</p> 
     </div> 

    </div> 

    <script> 
    // Get the modal 
    var modal = document.getElementById('myModal1'); 

    // 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 the button, open the modal 
    btn.onclick = function() { 
     modal.style.display = "block"; 
    } 

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



    </body> 
    </html> 
2つのモーダルと

私の第二の例は以下である。(動作しない)

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Modals</title> 
<style> 
/* The Modal (background) */ 
.modal { 
    display: none; /* Hidden by default */ 
    position: fixed; /* Stay in place */ 
    z-index: 1; /* Sit on top */ 
    padding-top: 100px; /* Location of the box */ 
    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 */ 
.modal-content { 
    background-color: #fefefe; 
    margin: auto; 
    padding: 20px; 
    border: 1px solid #888; 
    width: 80%; 
} 

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

.close:hover, 
.close:focus { 
    color: #000; 
    text-decoration: none; 
    cursor: pointer; 
} 
</style> 
</head> 
<body> 

<h2>Modal Example</h2> 

<!-- Trigger/Open The Modal --> 
<button id="myBtn" type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal</button> 

<!-- The Modal --> 
<div id="myModal1" class="modal"> 

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close">×</span> 
    <p>Some text in the Modal..</p> 
    </div> 

</div> 

<script> 
// Get the modal 
var modal = document.getElementById('myModal1'); 

// 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 the button, open the modal 
btn.onclick = function() { 
    modal.style.display = "block"; 
} 

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

    <h2>Modal Example 3</h2> 

<!-- Trigger/Open The Modal --> 
<button id="myBtn2" type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal</button> 

<!-- The Modal --> 
<div id="myModal2" class="modal"> 

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close">×</span> 
    <p>Some text in the Modal..</p> 
    </div> 

</div> 

<script> 
// Get the modal 
var modal = document.getElementById('myModal2'); 

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

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

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

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



</body> 
</html> 

答えて

1

最初のモーダルに対して定義した変数(btn、モーダル)およびonlickイベントを上書きしています。

2つ目のモーダルの変数の名前を変更することで問題を解決できます(例:modal2、btn2など)。

また、あなたがかつてwindow.onclickイベントを定義し、このような各モーダルかどうかを確認する必要があります。ここ

window.onclick = function(event) { 
    if (event.target == modal2) { 
     modal2.style.display = "none"; 
    } 
    if (event.target == modal) { 
     modal.style.display = "none"; 
    } 
} 

は迅速jsfiddleです:https://jsfiddle.net/0yccvpng/

+0

はありがとうございました!それは本当に助けになりました。これはコーディングの初期段階でモーダルとImを使用して初めてのことです! – gjem

関連する問題