gpt4 book ai didi

javascript - 我如何在 document.getElementsByClassName 中添加 2 个 div

转载 作者:太空宇宙 更新时间:2023-11-04 09:23:48 26 4
gpt4 key购买 nike

所以。这是我的问题。我要在我的网站上添加一个模式,可以通过至少 20 个按钮切换,但它不会是相同的模式,它们有不同的内容。

我试过这个:

var btn = document.getElementById("myBtn,myBtn2");

1 按钮通过按“myBtn”切换模态,第二个按钮不能有相同的 id,因为那样只会弹出 1。代码:

var modal = document.getElementById('myModal');

var btn = document.getElementById("myBtn""myBtn2");


var span = document.getElementsByClassName("close")[0];

btn.onclick = function() {
modal.style.display = "block";
}

span.onclick = function() {
modal.style.display = "none";
}

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

CSS:

.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 10; /* 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 {
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 */
}

.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}

html:

<div class="btn-more" id="myBtn">Open Modal</div>

这是完整的演示: http://www.w3schools.com/howto/howto_css_modals.asp

最佳答案

试试这个:

var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.querySelectorAll(".toToggle");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
for(var i=0;i<btn.length;i++){
btn[i].addEventListener("click", function() {
modal.style.display = "block";
document.querySelector(".modal-content").innerHTML += "<span class='close'>&times;</span><p>Some text in the Modal.. giv by button with id="+this.id+"</p>";
var numId = (this.id).replace("myBtn","");
if(numId == 0) {
document.querySelector(".modal-content").innerHTML += "<div class='imblock'><img src='https://static.pexels.com/photos/28993/pexels-photo.jpg'/></div>";
}
else if(numId == 1) {
document.querySelector(".modal-content").innerHTML += "<div class='imblock'><img src='https://static.pexels.com/photos/50704/car-race-ferrari-racing-car-pirelli-50704.jpeg'/></div>";
}
else if(numId == 2) {
document.querySelector(".modal-content").innerHTML += "<div class='imblock'><img src='https://i.ytimg.com/vi/4-PDH6sKsA0/hqdefault.jpg'/></div>";
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
document.querySelector(".modal-content").innerHTML = "";}
});
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
document.querySelector(".modal-content").innerHTML = "";
}
}
.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 */
}

/* 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;
}
.imblock img{
width:400px;
display:block;
margin:0;
padding:0:
}
<!-- Trigger/Open The Modal -->
<button class="toToggle" id="myBtn0">Open Modal-0</button>
<button class="toToggle" id="myBtn1">Open Modal-1</button>
<button class="toToggle" id="myBtn2">Open Modal-2</button>

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

<!-- Modal content -->
<div class="modal-content">
</div>

</div>

关于javascript - 我如何在 document.getElementsByClassName 中添加 2 个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309596/

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