gpt4 book ai didi

javascript - 使用 html css 和 javascript 创建弹出框

转载 作者:行者123 更新时间:2023-11-28 09:01:39 30 4
gpt4 key购买 nike

基本上,我正在尝试创建多个弹出框,这些弹出框会在单击不同的链接时出现。出于某种原因,只有在单击第一个链接时才会出现弹出框。单击其余链接时,什么也没有发生。感谢您的帮助,谢谢。此外,我在此示例中只包含了 2 个链接。

javascript代码:

function xpopup() {
document.getElementById("x").onclick= function(){
var overlay = document.getElementById("overLay");
var popup = document.getElementById("xPopup");
overlay.style.display = "block";
popup.style.display = "block";
}
}

function ypopup() {
document.getElementById("y").onclick= function(){
var overlay = document.getElementById("overLay");
var popup = document.getElementById("yPopup");
overlay.style.display = "block";
popup1.style.display = "block";
}
}
</script>

HTML代码:

 <body onLoad="xpopup()"; "ypopup()";>
<div id="overLay"></div>

<div class="popupBox" id="xPopup"></div>
<div class="popupBox" id="yPopup"></div>

<a href="#" class="listAttractions" id="x">Link 1</a><br>
<a href="#" class="listAttractions" id="y">Link 2</a><br>

CSS 代码:

.popupBox{
display:none;
position: fixed;
width: 30%;
height: 40%;
margin-left: 16.5%;
margin-top: 4.5%;
background-color: white;
border: 2px solid black;
border-radius: 10px;
z-index: 10;
}

#overLay{
display:none;
position: fixed;
width: 100%;
height: 100%;
background-color: #707070;
opacity: 0.7;
z-index: 9;
left: 0;
top: 0;
}

最佳答案

替换<body onLoad="xpopup()"; "ypopup()";><body onLoad="xpopup(); ypopup();">并且在您的 JavaScript 代码中有错字。

function ypopup() {
document.getElementById("y").onclick= function(){
var overlay = document.getElementById("overLay");
var popup = document.getElementById("yPopup");
overlay.style.display = "block";
popup1.style.display = "block"; // Here the popup1 is undefined change it to popup.style.....
}
}

编辑:-->

如果您单击灰色部分,我已经更改了您的代码以隐藏弹出窗口。

Fiddle

HTML:

<body>
<div id="overLay"></div>
<div class="popupBox" id="xPopup"></div>
<div class="popupBox" id="yPopup"></div>
<a href="#" class="listAttractions" id="x">Link 1</a>
<br />
<a href="#" class="listAttractions" id="y">Link 2</a>
<br />
</body>

JavaScript:

var overlay = document.getElementById("overLay");
var xpopup = document.getElementById("xPopup");
var ypopup = document.getElementById("yPopup");

document.getElementById("x").onclick = function () {
overlay.style.display = "block";
xpopup.style.display = "block";
};
document.getElementById("y").onclick = function () {
overlay.style.display = "block";
ypopup.style.display = "block";
};
overlay.onclick = function () {
overlay.style.display = "none";
xpopup.style.display = "none";
ypopup.style.display = "none";
};

关于javascript - 使用 html css 和 javascript 创建弹出框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26889028/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com