如何复制粘贴模态

时间:2017-11-15 13:23:16

标签: html css modal-dialog bootstrap-modal

对于一个学校项目,我想为班上的每个学生制作模态。我的问题是只有第一个模态才有效。



Traceback (most recent call last):
  File "sample.py", line 13, in <module>
gif.save("out.pdf", save_all=True, append_images=images)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 1928, in save
save_handler(self, fp, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 55, in _save_all
_save(im, fp, filename, save_all=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/PdfImagePlugin.py", line 182, in _save
Image.SAVE["JPEG"](im, op, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 609, in _save
info = im.encoderinfo
AttributeError: 'Image' object has no attribute 'encoderinfo'
&#13;
// 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";
  }
}
&#13;
/* The Modal (background) */

.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: 14% auto;
  /* 15% from the top and centered */
  padding: 20px;
  border: none;
  width: 80%;
  /* Could be more or less, depending on screen size */
  box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5);
}


/* 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;
}


/* Button Design */

#myBtn {
  background-image: url(/images/blank-profile-picture-973460_960_720.png);
  font-family: Lato-Bold;
  font-size: 20px;
  background-color: rgb(55, 154, 177);
  background-size: cover;
  width: 300px;
  height: 300px;
  margin: 10px;
  float: left;
  text-align: center;
  color: white;
  border: 0;
  cursor: pointer;
}

p.Info {
  font-family: Lato-regular;
  font-size: 15px;
  color: rgb(40, 40, 40);
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

这是一种更通用的方法,它依赖于dom元素的排序。

在这种情况下,如果您想添加更多内容,您只需复制按钮和模式html并更改内容即可。您不必更改代码,css或div id。

JSFiddle同样。

// Get the modals
var modals = document.getElementsByClassName('myModal');

// Get the buttons that opens the modals
var btns = document.getElementsByClassName("myBtn");

// Get the <span> elements that closes the modals
var spans = document.getElementsByClassName("close");

function makeShower(index) {
	return function(event) {
  	modals[index].style.display = "block";
  }
}

function makeHider(index) {
	return function(event) {
  	modals[index].style.display = "none";
  }
}

for (let i = 0; i < btns.length; i++) {
  btns[i].onclick = makeShower(i);
  spans[i].onclick = makeHider(i);
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target.className == "myModal modal") {
    event.target.style.display = "none";
  }
}
/* The Modal (background) */
.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: 14% auto; /* 15% from the top and centered */
    padding: 20px;
    border: none;
    width: 80%; /* Could be more or less, depending on screen size */
        box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5);
}

/* 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;
}
/* Button Design */
.myBtn {
  background-image: url(/images/blank-profile-picture-973460_960_720.png);
   font-family: Lato-Bold;
   font-size: 20px;
   background-color: rgb(55, 154, 177);
   background-size:cover;
   width: 300px;
   height: 300px;
   margin: 10px;
   float: left;
   text-align: center;
   color:white;
   border:0;
   cursor: pointer;
}

p.Info {
     font-family: Lato-regular;
     font-size: 15px;
     color: rgb(40, 40, 40);


}
  <!-- Trigger/Open The Modal -->
<button class="myBtn">Anthony</button>

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

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p class="Info">
Nachname:König<br>
Vorname:Anthony<br>
Geburtstag:24.12.1998<br>
Herkunft:Deutschland<br>
Adresse:xxxxxxxxxxxxxxxxx<br>
Mobilnummer:xxxxxxxxxxxxxx<br>
E-Mail:xxxxxxxxxxxxxxxxxbr>
Fachlicher Schwerpunkt:Programmieren<br>
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br>
Socialmedia:url Facebook: url Instagram:<br>
Snapcode: anthony.k1ng<br>
</p>
</div>

</div>
 <!-- Trigger/Open The Modal -->
<button class="myBtn">David</button>

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

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p class="Info">
Nachname:David<br>
Vorname:David<br>
Geburtstag:24.12.1998<br>
Herkunft:Deutschland<br>
Adresse:xxxxxxxxxxxxxxxxxxxxxx<br>
Mobilnummer:xxxxxxxxxxxxxxxxxx<br>
E-Mail:xxxxxxxxxxxxxxxx<br>
Fachlicher Schwerpunkt:Programmieren<br>
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br>
Socialmedia:url Facebook: url Instagram:<br>
Snapcode: anthony.k1ng<br>
</p>
</div>

</div>

答案 1 :(得分:0)

从你的代码中,你给了两个模态相同的id myModal。在html中,Id只能在页面上存在一次。 Id存在的第一个模态将是唯一触发的模式,因为同样的事情也适用于您的按钮。 更改按钮的单个Id以及模态,然后根据单击的按钮分别定位它们。对于你的关闭按钮也应该做同样的事情

将您的JavaScript更改为此

 var modal1 = document.getElementById('myModal1');
var modal2 = document.getElementById('myModal2');
// Get the button that opens the modal
var btn1 = document.getElementById("myBtn1");
var btn2 = document.getElementById("myBtn2");

// Get the <span> element that closes the modal
var span1 = document.getElementById("close1");
var span2 = document.getElementById("close2");
// When the user clicks on the button, open the modal
btn1.onclick = function() {
    modal1.style.display = "block";
}
btn2.onclick = function() {
    modal2.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span1.onclick = function() {
    modal1.style.display = "none";
}
span2.onclick = function() {
    modal2.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal1) {
        modal1.style.display = "none";
    }
    if (event.target == modal2) {
        modal2.style.display = "none";
    }
}

你的HTML

   <!-- Trigger/Open The Modal -->
<button id="myBtn1" class="myBtn">Anthony</button>

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

<!-- Modal content -->
<div class="modal-content">
<span class="close" id="close1">&times;</span>
<p class="Info">
Nachname:König<br>
Vorname:Anthony<br>
Geburtstag:24.12.1998<br>
Herkunft:Deutschland<br>
Adresse:xxxxxxxxxxxxxxxxx<br>
Mobilnummer:xxxxxxxxxxxxxx<br>
E-Mail:xxxxxxxxxxxxxxxxxbr>
Fachlicher Schwerpunkt:Programmieren<br>
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br>
Socialmedia:url Facebook: url Instagram:<br>
Snapcode: anthony.k1ng<br>
</p>
</div>

</div>
 <!-- Trigger/Open The Modal -->
<button id="myBtn2" class="myBtn">David</button>

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

<!-- Modal content -->
<div class="modal-content">
<span class="close" id="close2">&times;</span>
<p class="Info">
Nachname:König<br>
Vorname:Anthony<br>
Geburtstag:24.12.1998<br>
Herkunft:Deutschland<br>
Adresse:xxxxxxxxxxxxxxxxxxxxxx<br>
Mobilnummer:xxxxxxxxxxxxxxxxxx<br>
E-Mail:xxxxxxxxxxxxxxxx<br>
Fachlicher Schwerpunkt:Programmieren<br>
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br>
Socialmedia:url Facebook: url Instagram:<br>
Snapcode: anthony.k1ng<br>
</p>
</div>

</div>

然后是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: 14% auto; /* 15% from the top and centered */
    padding: 20px;
    border: none;
    width: 80%; /* Could be more or less, depending on screen size */
        box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5);
}

/* 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;
}
/* Button Design */
.myBtn {
  background-image: url(/images/blank-profile-picture-973460_960_720.png);
   font-family: Lato-Bold;
   font-size: 20px;
   background-color: rgb(55, 154, 177);
   background-size:cover;
   width: 300px;
   height: 300px;
   margin: 10px;
   float: left;
   text-align: center;
   color:white;
   border:0;
   cursor: pointer;
}

p.Info {
     font-family: Lato-regular;
     font-size: 15px;
     color: rgb(40, 40, 40);


}