gpt4 book ai didi

javascript - 单击时显示文本

转载 作者:行者123 更新时间:2023-12-05 04:38:09 27 4
gpt4 key购买 nike

在这个元素中,我的目标是在我点击时更改表情符号,并且我希望在我点击表情符号时显示“这不好笑兄弟”文本。当我点击表情符号时,它会发生变化,但文本不会显示。我遵循了相同的步骤,它适用于表情符号但不适用于文本。我不知道为什么它不起作用。我的代码如下所示:

const beginningFace = document.querySelector(".beginning");
const reactionFace = document.querySelector(".reaction");

const beginningText = document.querySelector(".beginningT");
const reactionTextFirst = document.querySelector(".reactionT-first");
const reactionTextSecond = document.querySelector(".reactionT-second");


beginningFace.addEventListener('click',()=>{

if(reactionFace.classList.contains('reaction')){
reactionFace.classList.add('active');
beginningFace.classList.remove('active');

}

});

beginningText.addEventListener('click',()=>{
if(reactionTextFirst.classList.contains('reactionT-first') && reactionTextSecond.classList.contains('reactionT-second')){
reactionTextFirst.classList.add('active');
reactionTextSecond.classList.add('active');
beginningFace.classList.remove('active');
}



});
@import url('https://fonts.googleapis.com/css2?family=Mr+Dafoe&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Exo:wght@900&display=swap');

body, html {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background-color: #202076;
}

body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
perspective: 700px;

}

.beginningT {
display: none;
}

.reactionT-first {
display:none;
position: relative;
font-family: 'Exo';
font-size: 5em;
margin: 0;
transform: skew(-15deg);
letter-spacing: 0.03em;
}

.reactionT-second{
display: none;
font-family: 'Mr Dafoe';
margin: 0;
font-size: 5.5em;
margin-top: -0.6em;
color: white;
text-shadow: 0 0 0.05em #fff, 0 0 0.2em #fe05e1, 0 0 0.3em #fe05e1;
transform: rotate(-7deg);
}

.emoji{
font-size: 18rem;
cursor: pointer;
user-select: none;

}


.beginning{
display: none;
}

.reaction{
display: none;
}

.active{
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>

<link rel="stylesheet" href="style.css">

</head>
<body>


<div class="emoji beginning active">😃</div>
<div class="emoji reaction">😐</div>

<div class ="text beginningT active" style="font-size: 2rem; font-style: italic; font-family: Verdana, Geneva, Tahoma, sans-serif; color: #F98299;">My reaction after your joke↑ (click to see it!)</div>
<div class="text reactionT-first">IT WASN'T</div>
<div class="text reactionT-second">FUNNY BRO</div>






<script src="app.js"></script>
</body>
</html>

最佳答案

如果您希望在单击表情符号时显示其他文本,则必须在 beginningFace.addEventListener 中执行所有逻辑,而不是使用两个不同的事件监听器。我移动了逻辑,并删除了第二个点击监听器。

第一个点击监听器中缺少这些行

    reactionTextFirst.classList.add('active');
reactionTextSecond.classList.add('active');
beginningText.classList.remove('active');

const beginningFace = document.querySelector(".beginning");
const reactionFace = document.querySelector(".reaction");

const beginningText = document.querySelector(".beginningT");
const reactionTextFirst = document.querySelector(".reactionT-first");
const reactionTextSecond = document.querySelector(".reactionT-second");


beginningFace.addEventListener('click',()=>{

if(reactionFace.classList.contains('reaction')){
reactionFace.classList.add('active');
beginningFace.classList.remove('active');

reactionTextFirst.classList.add('active');
reactionTextSecond.classList.add('active');
beginningText.classList.remove('active');
}

});
@import url('https://fonts.googleapis.com/css2?family=Mr+Dafoe&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Exo:wght@900&display=swap');

body, html {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background-color: #202076;
}

body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
perspective: 700px;

}

.beginningT {
display: none;
}

.reactionT-first {
display:none;
position: relative;
font-family: 'Exo';
font-size: 5em;
margin: 0;
transform: skew(-15deg);
letter-spacing: 0.03em;
}

.reactionT-second{
display: none;
font-family: 'Mr Dafoe';
margin: 0;
font-size: 5.5em;
margin-top: -0.6em;
color: white;
text-shadow: 0 0 0.05em #fff, 0 0 0.2em #fe05e1, 0 0 0.3em #fe05e1;
transform: rotate(-7deg);
}

.emoji{
font-size: 18rem;
cursor: pointer;
user-select: none;

}


.beginning{
display: none;
}

.reaction{
display: none;
}

.active{
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>

<link rel="stylesheet" href="style.css">

</head>
<body>


<div class="emoji beginning active">😃</div>
<div class="emoji reaction">😐</div>

<div class ="text beginningT active" style="font-size: 2rem; font-style: italic; font-family: Verdana, Geneva, Tahoma, sans-serif; color: #F98299;">My reaction after your joke↑ (click to see it!)</div>
<div class="text reactionT-first">IT WASN'T</div>
<div class="text reactionT-second">FUNNY BRO</div>






<script src="app.js"></script>
</body>
</html>

关于javascript - 单击时显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70635352/

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