Html/CSS/Javascript:弹出式 onclick 第二次无效 div
Html/CSS/Javascript: Popup onclick not working for second div
我在选择 div
时打开一个小弹出框。它有效,但如果我第二次尝试这样做(如下所示),它就不起作用。
感谢您提供的任何帮助,我们将不胜感激。这是我的代码 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>
您的代码似乎工作正常。
/* 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>
<script>
function myFunction() {
var popup =
document.getElementById("myPopup");
popup.classList.toggle("show");
}
function myFunction2() {
var popup =
document.getElementById("myPopup2");
popup.classList.toggle("show");
}
</script>
仅使用javascript添加点击事件即可。
使用它,您可以添加尽可能多的弹出元素。
var popupArr = document.getElementsByClassName("popup");
for (var index in popupArr) {
popupArr[index].onclick = function(ev) {
var popup = ev.target.children[0];
popup.classList.toggle("show");
}
}
/* 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">Click
me!
<span class="popuptext" >Popup
text...</span>
</div>
<div class="popup">Click
me2!
<span class="popuptext" >Popup
text...2</span>
</div>
我在选择 div
时打开一个小弹出框。它有效,但如果我第二次尝试这样做(如下所示),它就不起作用。
感谢您提供的任何帮助,我们将不胜感激。这是我的代码 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>
您的代码似乎工作正常。
/* 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>
<script>
function myFunction() {
var popup =
document.getElementById("myPopup");
popup.classList.toggle("show");
}
function myFunction2() {
var popup =
document.getElementById("myPopup2");
popup.classList.toggle("show");
}
</script>
仅使用javascript添加点击事件即可。 使用它,您可以添加尽可能多的弹出元素。
var popupArr = document.getElementsByClassName("popup");
for (var index in popupArr) {
popupArr[index].onclick = function(ev) {
var popup = ev.target.children[0];
popup.classList.toggle("show");
}
}
/* 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">Click
me!
<span class="popuptext" >Popup
text...</span>
</div>
<div class="popup">Click
me2!
<span class="popuptext" >Popup
text...2</span>
</div>