所以我有一个'我们的团队'页面,我想在点击他们的名字时显示每个成员的更多信息。
我有一个模态框代码,但是,它只适用于第一个成员。当我点击其他成员时#39;名字,没有任何弹出。
这是模式框html代码:
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<object width="100%" height="100%" data="bio/*insert member name*.pdf"></object>
</div>
</div>
现在为每个成员重复此代码,并更改插入成员名称部分以显示正确的pdf。
以下是引用模式框的html代码: 会员姓名
这是CSS代码:
.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 {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
height: 100%
}
/* 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;
以下是javascript代码:
// 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";
}
// 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";
}
}
请帮忙,因为我不知道我做错了什么。
提前谢谢。
答案 0 :(得分:0)
您只分享了部分代码,但我假设您的循环正在创建divs
个模式。
您可以创建一个接收modalIds数组的函数,并通过id应用监听器。
var setupModals = function(modalIds) {
for (var i = 0; i < modalIds.length; i++) {
setupModalListener(modalIds[i]);
}
};
var setupModalListener = function(modalId) {
var modal = document.getElementById(modalId);
// Get the <span> element that closes the modal
var span = modal.getElementsByClassName("close")[0];
// Get the button that opens the modal
var btn = document.getElementById(modalId + "_myBtn");
// When the user clicks on 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";
}
};
};
setupModals(['myModal', 'myModal2']);
&#13;
.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 {
background-color: #fefefe;
margin: 15% auto;
/* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%;
/* Could be more or less, depending on screen size */
height: 100%
}
/* 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;
&#13;
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<object width="100%" height="100%" data="bio/*insert member name*.pdf">
Hello world # 1
</object>
</div>
</div>
<div id="myModal2" class="modal">
<div class="modal-content">
<span class="close">×</span>
<object width="100%" height="100%" data="bio/*insert member name*.pdf">
Hello world # 2
</object>
</div>
</div>
<button id="myModal_myBtn">Click#1</button>
<button id="myModal2_myBtn">Click#2</button>
&#13;
希望有所帮助!