gpt4 book ai didi

javascript - 单击时如何显示文本?

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:10 25 4
gpt4 key购买 nike

单击这些自定义“按钮”时如何显示文本?我想这是一个 if 语句,但我不确定如何合并那和所有其他需要这样做的陈述,我已经尝试这样做但没有成功并且想知道是否有人可以提供帮助?

function alter() {
var button = document.getElementById('addButton');
button.setAttribute('class', 'cancelButton');
}

function alter2() {
var button = document.getElementById('leftButton');
button.setAttribute('class', 'cancelLeftButton');
}

function alter3() {
var button = document.getElementById('rightButton');
button.setAttribute('class', 'cancelRightButton');
}
#addButton {
background-color: black;
width: 100px;
height: 100px;
border-radius: 50%;
position: fixed;
top: 50%;
left: 50%;
margin: -50px;
font-size: 60px;
color: white;
text-align: center;
line-height: 90px;
}
.cancelButton {
animation: turnToCross 1s forwards cubic-bezier(0.000, 1.650, 1.000, 1.650);
}
@keyframes turnToCross {
to {
border-radius: 0;
transform: rotate(45deg);
background-color: #AD80FF;
}
from {}
}
#leftButton {
background-color: black;
width: 100px;
height: 100px;
border-radius: 50%;
position: fixed;
top: 50%;
left: 25%;
margin: -50px;
font-size: 60px;
color: white;
text-align: center;
line-height: 90px;
}
.cancelLeftButton {
animation: turnToCross 1s forwards cubic-bezier(0.000, 1.650, 1.000, 1.650);
}
@keyframes turnToCross {
to {
border-radius: 0;
transform: rotate(45deg);
background-color: #AD80FF;
}
from {}
}
#rightButton {
background-color: black;
width: 100px;
height: 100px;
border-radius: 50%;
position: fixed;
top: 50%;
right: 25%;
margin: -50px;
font-size: 60px;
color: white;
text-align: center;
line-height: 90px;
}
.cancelRightButton {
animation: turnToCross 1s forwards cubic-bezier(0.000, 1.650, 1.000, 1.650);
}
@keyframes turnToCross {
to {
border-radius: 0;
transform: rotate(45deg);
background-color: #AD80FF;
}
from {}
}
<body>
<div onclick="alter()" id="addButton">+</div>
<div onclick="alter2()" id="leftButton">+</div>
<div onclick="alter3()" id="rightButton">+</div>
</body>

最佳答案

首先您需要添加一个您希望显示文本的元素

  <body>
<div onclick="alter()" id="addButton">+</div>
<div onclick="alter2()" id="leftButton">+</div>
<div onclick="alter3()" id="rightButton">+</div>
<div id="showhereid "></div>
</body>

然后,在要点击的按钮函数中,你要插入这行代码

 document.getElementById('showhereid').innerHTML = "your text";

您的 javascript 代码将如下所示:

<script>
function alter() {
var button = document.getElementById('addButton');
button.setAttribute('class', 'cancelButton');
document.getElementById('showhereid').innerHTML = "your text";
}

function alter2() {
var button = document.getElementById('leftButton');
button.setAttribute('class', 'cancelLeftButton');
document.getElementById('showhereid').innerHTML = "your text";
}

function alter3() {
var button = document.getElementById('rightButton');
button.setAttribute('class', 'cancelRightButton');
document.getElementById('showhereid').innerHTML = "your text";
}
</script>

看看是否有帮助:)

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

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