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

时间:2016-12-24 01:39:53

标签: javascript html css

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

我试过了:

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

1按钮通过按“myBtn”切换模态,而secound按钮不能具有相同的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

1 个答案:

答案 0 :(得分:1)

试试这个:

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>