英文:
How can I close a popup when clicking another one?
问题
我有一个问题。
我有一系列弹出窗口,但我无法使打开的窗口在打开另一个窗口时关闭。
我希望在打开一个窗口时,另一个窗口关闭。
这是我的一部分代码:
<script>
function popup0(){
let popup = document.getElementById('popup0');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup0()'>El Gringo
<span class = 'popuptext' id = 'popup0'>
每当他因另一位玩家打出的卡牌失去一点生命点数时,格林戈就从该玩家的手中随机抽取一张卡牌(每点生命一张卡)。
<br>(3个生命点数)
</span>
</p><br>
<script>
function popup1(){
let popup = document.getElementById('popup1');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup1()'>Willy the Kid
<span class = 'popuptext' id = 'popup1'>
在他的回合中,他可以打出任意数量的“Bang!”卡牌。
<br>(4个生命点数)
</span>
</p><br>
<script>
function popup2(){
let popup = document.getElementById('popup2');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup2()'>Lucky Duke
<span class = 'popuptext' id = 'popup2'>
每当他需要“抽牌”时,他会翻动牌堆的前两张牌,并选择他喜欢的结果。然后将两张牌丢弃。
<br>(4个生命点数)
</span>
</p><br>
<script>
function popup3(){
let popup = document.getElementById('popup3');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup3()'>Kit Carlson
<span class = 'popuptext' id = 'popup3'>
在他的回合的第1阶段,他查看牌堆顶部的前三张牌,然后选择两张放入手中,将剩下的一张卡背面朝下放回牌堆。
<br>(4个生命点数)
</span>
</p><br>
英文:
I have a problem.
I have a series of popups and I am not able to make the opened one closing when opening another.
I'd like to have a result in which I open one of them and it closes when I open another.
Here's a piece of my code:
<script>
function popup0(){
let popup = document.getElementById('popup0');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup0()'>El Gringo
<span class = 'popuptext' id = 'popup0'>
Each time he loses a life point due to a card played by another player, Gringo draws a random card from the hand of that player (one card for each life).<br>(3 life points)
</span>
</p><br>
<script>
function popup1(){
let popup = document.getElementById('popup1');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup1()'>Willy the Kid
<span class = 'popuptext' id = 'popup1'>
During his turn, he can play any number of 'Bang!' cards.<br>(4 life points)
</span>
</p><br>
<script>
function popup2(){
let popup = document.getElementById('popup2');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup2()'>Lucky Duke
<span class = 'popuptext' id = 'popup2'>
Each time he is required to 'Draw!', he flips the top two cards from the deck and chooses the result he prefers. Both cards are discarded afterwards.<br>(4 life points)
</span>
</p><br>
<script>
function popup3(){
let popup = document.getElementById('popup3');
popup.classList.toggle('show');
}
</script>
<p class = 'popup' onclick = 'popup3()'>Kit Carlson
<span class = 'popuptext' id = 'popup3'>
During Phase 1 of his turn, he looks at the top three cards of the deck, then chooses two to put into his hand and puts the remaining card back onto the deck, face down.<br>(4 life points)
</span>
</p><br>
答案1
得分: 1
你可以创建一个新的函数来隐藏已打开的弹出窗口,并在每个函数内调用它。
像这样:
function hideOther(popupElem) {
document.querySelectorAll('.popuptext:not(#'+popupElem+')').forEach(elem => {
elem.classList.remove('show');
});
}
function popup0() {
let popup = document.getElementById('popup0');
hideOther('popup0');
popup.classList.toggle('show');
}
你需要在每个弹出窗口函数内调用hideOther
,就像我们在popup0
内调用它一样。
更新 将当前的ID传递给函数并检查以获取其他要关闭的弹出窗口。
英文:
You can just create a new function to hide opened popups and call it inside each function.
Like below:
function hideOther(popupElem) {
document.querySelectorAll('.popuptext:not(#'+popupElem+')').forEach(elem => {
elem.classList.remove('show');
});
}
function popup0() {
let popup = document.getElementById('popup0');
hideOther('popup0');
popup.classList.toggle('show');
}
You need to call hideOther
inside each popup function like here we called it inside popup0
Updated Passed current id to the function and checked to get only other popups to close
答案2
得分: 1
我已更改代码以具有弹出窗口ID的列表,使用这些ID,您可以在单击按钮以显示特定弹出窗口时停用所有其他弹出窗口。
const popupList = [
'popup1',
'popup2',
'popup3',
];
function showPopup(popupId){
let popup = document.getElementById(popupId);
popupList.forEach((popupElementId) => {
if(popupElementId !== popupId) {
document.getElementById(popupElementId).classList.remove('show');
}
});
popup.classList.toggle('show');
console.log('show ' + popupId);
}
链接到一个带有工作示例的在线fiddle:
https://jsfiddle.net/vmbjwna2/
英文:
I changed the code to have a list of popup ids, with this ids you can deactivate all other popup when a button is clicked to show a specific popup.
const poppupList = [
'popup1',
'popup2',
'popup3',
];
function showPopup(popupId){
let popup = document.getElementById(popupId);
poppupList.forEach((poppupElementId) => {
if(poppupElementId !== popupId) {
document.getElementById(poppupElementId).classList.remove('show');
}
});
popup.classList.toggle('show');
console.log('show ' + popupId);
}
Link to a online fiddle with a working example:
https://jsfiddle.net/vmbjwna2/
答案3
得分: 0
只需查找具有活动类的元素并将其移除。我已更改代码,不再使用内联事件,而是使用数据属性来确定要显示或移除的内容,以减少重复的代码。
function popup(elem) {
const toggles = document.querySelector(elem.dataset.toggles);
const active = document.querySelector('.popuptext.show');
if (active && active !== toggles) {
active.classList.remove('show');
}
toggles.classList.toggle('show');
}
document.querySelectorAll("[data-toggles]").forEach(elem => {
elem.addEventListener("click", () => popup(elem));
});
.popuptext {
display: none;
}
.popuptext.show {
display: block;
}
<p class='popup' data-toggles="#popup1">Willy the Kid
<span class='popuptext' id='popup1'>
在他的回合中,他可以打出任意数量的 'Bang!' 卡片。<br>(4 生命值)
</span>
</p><br>
<p class='popup' data-toggles="#popup2">Lucky Duke
<span class='popuptext' id='popup2'>
每当需要他 '抽牌!' 时,他会翻动牌堆顶部的两张牌,并选择他喜欢的结果。之后两张牌都被丢弃。<br>(4 生命值)
</span>
</p><br>
英文:
Just look up the element with the active class and remove it. I changed the code to not use inline events and use data attribute to determine what to show to remove a lot of repeated code.
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
function popup(elem) {
const toggles = document.querySelector(elem.dataset.toggles);
const active = document.querySelector('.popuptext.show');
if (active && active !== toggles) {
active.classList.remove('show');
}
toggles.classList.toggle('show');
}
document.querySelectorAll("[data-toggles]").forEach(elem => {
elem.addEventListener("click", () => popup(elem));
});
<!-- language: lang-css -->
.popuptext {
display: none;
}
.popuptext.show {
display: block;
}
<!-- language: lang-html -->
<p class='popup' data-toggles="#popup1">Willy the Kid
<span class='popuptext' id='popup1'>
During his turn, he can play any number of 'Bang!' cards.<br>(4 life points)
</span>
</p><br>
<p class='popup' data-toggles="#popup2">Lucky Duke
<span class='popuptext' id='popup2'>
Each time he is required to 'Draw!', he flips the top two cards from the deck and chooses the result he prefers. Both cards are discarded afterwards.<br>(4 life points)
</span>
</p><br>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论