gpt4 book ai didi

单击按钮时的 HTML 弹出框

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:36 25 4
gpt4 key购买 nike

我想在单击按钮后打开一个 HTML 弹出窗口。下面是我正在使用的代码,但它不起作用。我认为我将值传递给 span 标记的方式有问题:

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

/* The actual popup */
.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 - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}

@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
</style>
</head>
<body style="text-align:center">

<h2>Popup</h2>
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
<div class="popup" >
<span class="popuptext" id="myPopup">Ankit</span>
</div>
<p id="demo"></p>

<script>
// When the user clicks on div, open the popup
document.getElementById("myPopup").innerHTML= "Welcome " + name + ", the " + job + ".";
function myFunction(name,job) {
document.getElementById("myPopup").innerHTML =
"Welcome " + name + ", the " + job + ".";
var popup =document.getElementById("myPopup");
popup.classList.toggle("show");
}
</script>

</body>
</html>

如果我在 Span 标记中对值进行硬编码,代码可以正常工作并显示该值,但它不接受函数中传递的值。

最佳答案

不确定你想要什么作为你的输出,但是正确的代码方式如下:

<h2>Popup</h2>

<div class="popup" onclick="myFunction('Harry Potter','Wizard')">
<button> Try it </button>
<span class="popuptext" id="myPopup">Ankit </span>
</div>

<p id="demo"></p>

<script>
// When the user clicks on div, open the popup
document.getElementById("myPopup").innerHTML= "Welcome " + name + ", the " + job + ".";
function myFunction(name,job) {

var popup =document.getElementById("myPopup");
popup.innerHTML = "Welcome " + name + ", the " + job + ".";
popup.classList.toggle("show");
}
</script>

关于这段代码,

点击“试一试”按钮后,您将看到一个弹出窗口“欢迎哈利·波特,巫师”。原因是您将值“Harry Potter”作为名称传递,将“Wizard”作为工作传递onclick="myFunction('哈利波特','巫师')"

现在,在函数中,您声明了一个名为 popup 的变量,它存储来自 id“myPopup”的值,在本例中现在是“Ankit”

在下一行中,我们将值更改为“Welcome”+ name +“,”+ job +“.”,将行变为“Welcome Harry Potter, the Wizard”。值“Ankit”丢失了。

popup.classList.toggle("show"); 只是创建弹出窗口。

如果你希望在弹出窗口中也有“Ankit”,你可能希望尝试这个片段:

<div class="popup" onclick="myFunction('Harry Potter','Wizard')">
<button> Try it </button>
<span class="popuptext" id="myPopup"> Hi, I am Ankit. </span>
</div>

<p id="demo"></p>

<script>
// When the user clicks on div, open the popup
document.getElementById("myPopup").innerHTML= "Welcome " + name + ", the " + job + ".";
function myFunction(name,job) {

var popup =document.getElementById("myPopup");
popup.innerHTML = popup.innerHTML + " Welcome " + name + ", the " + job + ".";
popup.classList.toggle("show");
}
</script>

弹出窗口现在将显示为“嗨,我是 Ankit。欢迎哈利波特,巫师”。

希望这在某种程度上有所帮助:)

关于单击按钮时的 HTML 弹出框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53464889/

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