页面上有多个模态框
More than one modal box on page?
编辑
我已经更新了这个问题中的所有代码。
问题
我一直在尝试让多个模态框在一个页面上工作。
我可以让一个工作,但我不能复制它,这样两个就会打开(我希望最后有三个模态框?
这是被调用来打开第一个模态的按钮;
<button id="myHistoryBtn" class="myAboutBtn">more</button>
这是第二个;
<button id="myAboutBtn" class="myAboutBtn">more</button>
这是我使用的模态结构
<div id="myHistoryModal" class="modal">
<!-- Modal content -->
<div class="modal-container">
<div class="modal-content">
<span class="modalclose">×</span>
<h1>sstory</h1>
</div>
</div>
</div>
这是的第二个
<div id="myAboutModal" class="modal2">
<!-- Modal content -->
<div class="modal-container">
<div class="modal-content">
<span class="Aboutclose" id="close">×</span>
<h1>AAAAAstory</h1>
</div>
</div>
</div>
这就是让一切正常工作的JavaScript。
// Get the modal
var modal = document.getElementById('myHistoryModal');
// Get the button that opens the modal
var btn = document.getElementById("myHistoryBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("modalclose")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
main.style.display = "none";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
main.style.display = "flex";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
main.style.display = "flex";
}
}
下面是让其他模态框工作的JavaScript。
// Get the modal
var about = document.getElementById('myAboutModal');
// Get the button that opens the modal
var Abtn = document.getElementById("myAboutBtn");
// Get the <span> element that closes the modal
var Aspan = document.getElementById("close")[0];
// When the user clicks the button, open the modal
Abtn.onclick = function(event) {
about.style.display = "block";
main.style.display = "none";
}
// When the user clicks on <span> (x), close the modal
Aspan.onclick = function() {
about.style.display = "none";
main.style.display = "flex";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function() {
if (event.target == about) {
about.style.display = "none";
main.style.display = "flex";
}
}
我想我快疯了,因为现在一切都开始混乱起来了。一旦我尝试有一个以上的模式框它。
我又试了一次,我有点让它工作了(有点)
其中一个模式框在我点击按钮时出现,但在关闭时不会消失(只是停留在那里)
当我单击其他模式框按钮时,它会隐藏内容,但不显示模式框?
这是我正在做的工作的链接。我已经拿出了使其他模态框工作的代码,向你展示当你点击更多按钮时会发生什么。
http://www.periodictablesearch.zackreid.com/pages/Carbon.html
这让我现在很困惑。我已经重命名了第二组JavaScript上的所有内容,使第二个按钮可以工作,但仍然没有。
编辑
所以我现在让它们出现了,但其中一个仍然有点坏。
http://www.periodictablesearch.zackreid.com/pages/Carbon.html
然后出现第二个模态框,但它不会隐藏/关闭。当我们在模态框外点击按钮时,它完成了。
编辑
我现在已经获得了在点击窗口时关闭的第二个模态。只需要让按钮工作吗?有人感兴趣吗?我应该更新我的代码吗?
您可能需要将第二个盒子的名称更改为
var myAboutModal = document.getElementById('myAboutModal');
var myAboutBtn = document.getElementById("myAboutsBtn");
并将CCD_ 1注册为CCD_
myAboutBtn.onclick = function() {
myAboutModal.style.display = "block";
main.style.display = "none";
}
否则,它将与第一个btn
和modal
变量发生冲突
与span
变量相同。顺便说一句,跨度可能需要使用id
而不是类名,因为getElementsByClassName("close")[0];
总是文档中的第一个跨度,可能永远不会为第二个模态提供您想要的第二个跨度
对于关闭btn,您可以将事件侦听器添加到所有关闭按钮,而无需逐个添加
var closeBtns = document.getElementsByClassName("modalclose");
for(var i=0; i< closeBtns.length; i++) {
closeBtns[i].onclick = function() {
modal.style.display = "none";
about.style.display = "none";
}
}
要在单击模式外部时关闭模式,您需要检查目标是否不是任何模式、触发模式按钮、modal-container
、modal-content
以及onclick
0 内部的任何内容
window.onclick = function(event) {
if (event.target != modal && event.target != about && event.target != btn && event.target != Abtn && event.target.className != "modal-container" && event.target.className != "modal-content" && event.target.parentNode.className != "modal-content") {
modal.style.display = "none";
about.style.display = "none";
}
}
我不确定你想用main.style.display = "flex"
做什么。在大多数情况下,假设main
是html <body>
,则不需要设置任何关于main
的样式
一个正在工作的小提琴在这里https://jsfiddle.net/yg88rvgq/1/
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 打开一个模态并将其链接到AngularJS中的指令
- SemanticUI模态not onDeny/onApprove事件未激发
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- 包含数据库中相关信息的开放模态
- bootstrap消除模态并显示另一个模态
- 模态提示-如何重写此代码
- 模态框无法正常工作
- 将数据传递给由服务创建的Ionic模态
- iframe模态无法关闭
- 点击AngularJS模态窗口捕捉背景事件
- jqueryajax调用是复制url,当在模态视图之后在外部js文件中进行调用时
- 使用模态对话框(JQuery)编辑函数
- 如何在模态实例中使用filter
- 页面上有多个模态框
- 如果满足某些条件,如何在不调用模态的情况下首先调用类
- 在将缩略图链接到模态时遇到问题
- jQuery模态弹出的行为类似于确认和警报对话框
- 使用jQuery防止模态内部的元素关闭模态