javascript - OnClick 单个 JavaScript 弹出窗口

标签 javascript html css onclick popup

我一直在玩这段代码,但遇到了一些麻烦。我有它,至少到目前为止,当选择文本时会显示一个弹出窗口。问题是我无法让它对所有 4 个都起作用,所以我得到 4 个不同的弹出窗口,所有弹出窗口都连接到第一个。我也在尝试获取它,以便弹出窗口显示在每个单击的按钮下方。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.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: -250%;
    left: 50%;
    margin-left: -80px;
}

.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

</style>
</head>
<body style="text-align:center">

<div class="popup" onclick="myFunction()">First Popup
  <span class="popuptext" id="myPopup">This is the first pop up selected</span>
</div>

<div class="popup" onclick="myFunction()">second Popup
  <span class="popuptext" id="myPopup">This is the second pop up selected</span>
</div>

<div class="popup" onclick="myFunction()">third Popup
  <span class="popuptext" id="myPopup">This is the third pop up selected</span>
</div>

<div class="popup" onclick="myFunction()">Fourth Popup
  <span class="popuptext" id="myPopup">This is the fouth pop up selected</span>
</div>

<script>
function myFunction() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}
</script>

</body>
</html>

我的问题是如何才能让弹出窗口正确显示在每个文本下方?

谢谢。

最佳答案

您为每个弹出窗口使用了相同的 ID。你不能那样做。每个弹出窗口都需要有一个唯一的 ID。

关于javascript - OnClick 单个 JavaScript 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123403/

相关文章:

php - JS 改变值,PHP 获取旧值

javascript - 仅在必要时将数字四舍五入至小数点后两位

html - 为什么这两张图片不想叠加在一起?

html - bootstrap 复选框和标签在同一行

html - 我如何只显示一半的 div?

javascript - 如何在网页上动态突出显示字符串?

javascript - 当用户单击动态表时,使用 Jquery 检索动态表的行信息

python - 如何为 Pandas Dataframe 定义 html id

jquery - 中心的 float div 在 IE 中不起作用

javascript - 为什么清除 Canvas 元素并将其放回原处会出现故障?