0
私が見ての通り、私はスタックを初めて見ましたので間違いをお許しください。ボタンが異なる複数のブートストラップモーダルは動作しないそれぞれのコンテンツにリンクします
私はBootstrap3で私の友人のビジネスサイトを作っています。(会社の)チームページにはたくさんのメンバーがいますので、各メンバーにボタンでモーダルを詳しく説明したいと思います。
私はw3cサイトのモーダルサンプルコードを使用しました。私は別のメンバプロファイルボックスで最初の人のモーダル作品のみを使用しますが、残りのモーダルは動作しません。
ここにはモーダル作業コードが1つしかありません。 CSS
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: absolute; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 50px; /* 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 {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 98%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 10px 2px 0px 2px;
font-size: 20px;
font-weight: bold;
}
.modal-body {padding: 2px 2px;}
}
</style>
HTML
<div class="col-md-3 col-sm-6">
<div class="center">
<div class="panel panel-default">
<div class="ourteam-image">
<img src="images/portfolio/thumb/person1.gif" alt="">
</div>
<div class="panel-body">
<h4>Person 1</h4>
<h5>Director</h5>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
<!-- Trigger/Open The Modal -->
<button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal1">Open Modal</button>
<!-- The Modal -->
<div id="myModal1" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<p>Person 1</p>
</div>
<div class="modal-body">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
</div>
</div>
</div>
</div>
</div><!--/panel-->
</div><!--/center-->
</div><!--/col-->
<div class="col-md-3 col-sm-6">
<div class="center">
<div class="panel panel-default">
<div class="ourteam-image">
<img src="images/portfolio/thumb/person2.gif" alt="">
</div>
<div class="panel-body">
<h4>Person 2</h4>
<h5>Director</h5>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
<!-- Trigger/Open The Modal -->
<button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal2">Open Modal</button>
<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<p>Person 2</p>
</div>
<div class="modal-body">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
</div>
</div>
</div>
</div>
</div><!--/panel-->
</div><!--/center-->
</div><!--/col-->
<div class="col-md-3 col-sm-6">
<div class="center">
<div class="panel panel-default">
<div class="ourteam-image">
<img src="images/portfolio/thumb/person3.gif" alt="">
</div>
<div class="panel-body">
<h4>Person 3</h4>
<h5>Director</h5>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
<!-- Trigger/Open The Modal -->
<button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal3">Open Modal</button>
<!-- The Modal -->
<div id="myModal3" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<p>Person 3</p>
</div>
<div class="modal-body">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
</div>
</div>
</div>
</div>
</div><!--/panel-->
</div><!--/center-->
</div><!--/col-->
スクリプト
// Get the modal
var modal = document.getElementById('myModal1','myModal2','myModal3');
// 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>
長いポスト申し訳ありません
ちょうどそれを明確にしたい。 ヘルプや修正は本当に素晴らしいでしょう。 ありがとう
'document.getElementById( 'myModal1'、 'myModal2'、 'myModal3')'?あなたはこれを行うことはできません.. –
あなたのコードはjavascriptを必要とせずに正常に動作します...わずか2つの小さな問題があります - '.modal' CSSで' z-index'を削除し、 'data-dismiss =あなたの 'close'ボタンには" modal "属性があります。スクリプトは必要ありません。 [JSFiddle demo](https://jsfiddle.net/8schp8g4/)をチェックしてください –
両方とも素早い返信をありがとうございます。 @ ARTUR-FILIPIAKモーダルbcoz JSFiddleを使用していても、同じコードでもJSFiddleコードだけでは新しいページが作成されない可能性はありますか? –