gpt4 book ai didi

Javascript addEventListener 只运行一次

转载 作者:行者123 更新时间:2023-12-01 17:30:26 28 4
gpt4 key购买 nike

我一直在尝试构建一个非常简单的网站,该网站会在用户点击 UI 中代表的建筑物时弹出信息卡,并在用户点击 Angular 落的罗盘 img 时移除信息卡.该代码应用两个不同的 CSS 类来使卡片出现和消失。我能够使 addEventListener 在建筑物和指南针上都起作用,但它只能起作用一次。换句话说,点击一次建筑物后,卡片出现,点击罗盘后消失。但是,前两次点击之后的任何进一步点击似乎都没有注册。至少,卡片不会再出现和消失。

这看起来很简单,但我一直无法弄清楚为什么按钮只能工作一次。

我一直在 SublimeText 中写作,并在我的浏览器中查看网站。如果您对发生这种情况的原因有任何想法,请告诉我?!

//stores marker as variable
var wsc = document.getElementById("wsc");
//stores event function
var onBuildingClick = function() {
document.getElementById("_wsc").classList.add("card");
}
//adds event listener to marker
wsc.addEventListener("click", onBuildingClick);



//////////////////////////////////


//stores compass as a variable
var compass = document.getElementById("compass_img");
//stores event function
var onCompassClick = function() {
document.getElementById("_wsc").classList.add("_card");
}
//adds event listener to compass
compass.addEventListener("click", onCompassClick);
.card {
position: absolute;
background-color: white;
width: 60%;
height: 60%;
left: 20%;
top: 20%;
}
._card {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="byu_richmap.css">

<title>byu_richmap</title>
</head>

<body>
<div class="campus_map">
<img id="campus_img" src="BYUmap_simple.png">
<div id="wsc" class="identifier"></div>
<div id="hbll" class="identifier"></div>
<div id="hfac" class="identifier"></div>
<div id="moa" class="identifier"></div>
<div id="asb" class="identifier"></div>
<div id="jkb" class="identifier"></div>
<div id="tmcb" class="identifier"></div>
<div id="jfsb" class="identifier"></div>
<div id="swkt" class="identifier"></div>
<div id="esc" class="identifier"></div>
<div id="kc" class="identifier"></div>
<div id="marb" class="identifier"></div>
<div id="cb" class="identifier"></div>
<div id="ctb" class="identifier"></div>
<div id="lsb" class="identifier"></div>
<div id="brwb" class="identifier"></div>
<div id="snlb" class="identifier"></div>
<div id="rotc" class="identifier"></div>
<div id="jrcb" class="identifier"></div>
<div id="mlbm" class="identifier"></div>
<div id="mc" class="identifier"></div>
<div id="hc" class="identifier"></div>
<div id="tnrb" class="identifier"></div>
<div id="bnsb" class="identifier"></div>
<div id="hgb" class="identifier"></div>
<div id="msrb" class="identifier"></div>
<div id="brmb" class="identifier"></div>
<div id="mckb" class="identifier"></div>
<div id="rb" class="identifier"></div>
<div id="lves" class="identifier"></div>
<div id="canc" class="identifier"></div>
<div id="sab" class="identifier"></div>
<div id="sfh" class="identifier"></div>

<div id="_wsc" class="cards"></div>
<!--<div id="_hbll" class="card"></div>
<div id="_hfac" class="card"></div>
<div id="_moa" class="card"></div>
<div id="_asb" class="card"></div>
<div id="_jkb" class="card"></div>
<div id="_tmcb" class="card"></div>
<div id="_jfsb" class="card"></div>
<div id="_swkt" class="card"></div>
<div id="_esc" class="card"></div>
<div id="_kc" class="card"></div>
<div id="_marb" class="card"></div>
<div id="_cb" class="card"></div>
<div id="_ctb" class="card"></div>
<div id="_lsb" class="card"></div>
<div id="_brwb" class="card"></div>
<div id="_snlb" class="card"></div>
<div id="_rotc" class="card"></div>
<div id="_jrcb" class="card"></div>
<div id="_mlbm" class="card"></div>
<div id="_mc" class="card"></div>
<div id="_hc" class="card"></div>
<div id="_tnrb" class="card"></div>
<div id="_bnsb" class="card"></div>
<div id="_hgb" class="card"></div>
<div id="_msrb" class="card"></div>
<div id="_brmb" class="card"></div>
<div id="_mckb" class="card"></div>
<div id="_rb" class="card"></div>
<div id="_lves" class="card"></div>
<div id="_canc" class="card"></div>
<div id="_sab" class="card"></div>
<div id="_sfh" class="card"></div>-->
</div>
<div class="compass">
<img id="compass_img" src="byu_richmap_compass.png">
</div>

<script src="byu_richmap.js"></script>
</body>

</html>

最佳答案

您没有清除使其不可见的 _card 样式。您的代码运行良好

var onBuildingClick = function() {
document.getElementById("_wsc").classList.add("card");
document.getElementById("_wsc").classList.remove("_card");
}

这里也是。

var onCompassClick = function() {
document.getElementById("_wsc").classList.add("_card");
document.getElementById("_wsc").classList.remove("card");
}

Fiddle

关于Javascript addEventListener 只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48466887/

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