gpt4 book ai didi

javascript - 如何创建多个模态来显示不同的数据?

转载 作者:行者123 更新时间:2023-12-03 05:45:38 26 4
gpt4 key购买 nike

我正在尝试创建多个模式来显示不同的数据。我查看了其他示例(关于堆栈溢出),并实现了代码,但它仍然对我不起作用。基本上在这一点上,我只是想让它发挥作用,并从 WW3 Schools 网页上复制了脚本。我有两个例子......一个只有一个模态(有效),第二个有两个模态(应该显示不同的数据(不起作用)。但是当我删除第一个模态时,第二个模态有效。我已经包括整个页面以防万一出现另一个问题,我还检查了元素并说没有错误。我希望你能帮助我!

第一个带有一个模态的示例(有效)。

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

下面是我的第二个带有两个模式的示例。 (不起作用)

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

最佳答案

您正在覆盖为第一个模态定义的变量(btn、模态)和 onlick 事件。

您可以通过重命名第二个模态的变量来解决问题(例如: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/

关于javascript - 如何创建多个模态来显示不同的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40339271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com