div
选择a 时,我正在打开一个小弹出框。它有效,但如果我尝试第二次执行此操作(如下所示),则无效。
感谢您提供的任何帮助,不胜感激。这是我来自w3schools 的代码:
HTML:
<div class="popup" onclick="myFunction()">
Click me!
<span class="popuptext" id="myPopup">Popup text...</span>
</div>
<div class="popup" onclick="myFunction2()">
Click me2!
<span class="popuptext" id="myPopup2">Popup text...2</span>
</div>
JavaScript:
function myFunction() {
var popup =
document.getElementById("myPopup");
popup.classList.toggle("show");
}
function myFunction2() {
var popup =
document.getElementById("myPopup2");
popup.classList.toggle("show");
}
CSS:
/* Popup container */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
}
/* The actual popup (appears on top) */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class when clicking on the popup
container (hide and show the popup) */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s
}
myFunction
声明了两次,第二个覆盖了第一个。
将其更改为myFunction2
或任何它们更新您的onclick="myFunction2()"
检查您的控制台是否有错误总是好的:)
function myFunction() {
var popup =
document.getElementById("myPopup");
popup.classList.toggle("show");
}
function myFunction2() {
var popup =
document.getElementById("myPopup2");
popup.classList.toggle("show");
}
body {
padding-top: 80px;
text-align: center;
}
/* Popup container */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
}
/* The actual popup (appears on top) */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent
transparent;
}
/* Toggle this class when clicking on the popup
container (hide and show the popup) */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s
}
<div class="popup" onclick="myFunction()">Click
me!
<span class="popuptext" id="myPopup">Popup
text...</span>
</div>
<div class="popup" onclick="myFunction2()">Click
me2!
<span class="popuptext" id="myPopup2">Popup
text...2</span>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句