Javascript 点击按钮时保持执行。多姆
我想让代码不断变化,而不是只执行一次。 默认情况下,按钮的文本模式为深色,背景色为粉红色。 单击按钮时,背景色将为黑色,按钮中的文本为黑色 将是灯光模式 我想当你用灯光模式点击按钮时, 反之亦然。 如何继续调用该函数Javascript 点击按钮时保持执行。多姆,javascript,html,dom,Javascript,Html,Dom,我想让代码不断变化,而不是只执行一次。 默认情况下,按钮的文本模式为深色,背景色为粉红色。 单击按钮时,背景色将为黑色,按钮中的文本为黑色 将是灯光模式 我想当你用灯光模式点击按钮时, 反之亦然。 如何继续调用该函数 函数changeLight(){ const btnTag=document.createElement('button'); document.body.style.backgroundColor=“粉色”; btnTag.style.backgroundColor=“白色”;
函数changeLight(){
const btnTag=document.createElement('button');
document.body.style.backgroundColor=“粉色”;
btnTag.style.backgroundColor=“白色”;
btnTag.textContent=“暗模式”;
btnTag.style.color=“黑色”;
btnTag.onclick=函数(){
如果(btnTag.textContent=“灯光模式”){
document.body.style.backgroundColor=“粉色”;
btnTag.style.backgroundColor=“白色”;
btnTag.textContent=“暗模式”;
btnTag.style.color=“黑色”;
}
如果(btnTag.textContent=“暗模式”){
document.body.style.backgroundColor=“黑色”;
btnTag.style.backgroundColor=“灰色”;
btnTag.textContent=“灯光模式”;
btnTag.style.color=“白色”;
}
}
document.body.appendChild(btnTag);
};
changeLight()代码>您的if语句应该与=
进行比较,而不是与=
进行赋值。另外,在代码中放入一个debugger()
或一个,并逐行检查每一行代码
然后,您会看到代码在第二次单击后进入第一个if语句,然后进入第二个if语句,因为您将文本更改为“暗模式”
您需要在第二条if语句前面添加else
:
else if (btnTag.textContent == "dark mode") {
函数changeLight(){
const btnTag=document.createElement('button');
document.body.style.backgroundColor=“粉色”;
btnTag.style.backgroundColor=“白色”;
btnTag.textContent=“暗模式”;
btnTag.style.color=“黑色”;
btnTag.onclick=函数(){
如果(btnTag.textContent==“灯光模式”){//已更改
document.body.style.backgroundColor=“粉色”;
btnTag.style.backgroundColor=“白色”;
btnTag.textContent=“暗模式”;
btnTag.style.color=“黑色”;
}
否则,如果(btnTag.textContent==“暗模式”){//已更改
document.body.style.backgroundColor=“黑色”;
btnTag.style.backgroundColor=“灰色”;
btnTag.textContent=“灯光模式”;
btnTag.style.color=“白色”;
}
}
document.body.appendChild(btnTag);
};
changeLight()代码>您在if条件中缺少“==”。我添加了else块,效果很好
btnTag.onclick = function(e) {
if(btnTag.textContent === "light mode") {
document.body.style.backgroundColor = "pink";
btnTag.style.backgroundColor = "white";
btnTag.textContent = "dark mode";
btnTag.style.color = "black";
} else
if (btnTag.textContent === "dark mode") {
document.body.style.backgroundColor = "black";
btnTag.style.backgroundColor = "gray";
btnTag.textContent = "light mode";
btnTag.style.color = "white";
}
}
您需要使用==
或==
进行比较,而不是使用=
。你在你的情况下使用过它
这应该行得通-
function changeLight() {
const btnTag = document.createElement('button');
document.body.style.backgroundColor = "pink";
btnTag.style.backgroundColor = "white";
btnTag.textContent = "dark mode";
btnTag.style.color = "black";
btnTag.onclick = function() {
if(btnTag.textContent === "light mode") {
document.body.style.backgroundColor = "pink";
btnTag.style.backgroundColor = "white";
btnTag.textContent = "dark mode";
btnTag.style.color = "black";
}
if(btnTag.textContent === "dark mode") {
document.body.style.backgroundColor = "black";
btnTag.style.backgroundColor = "gray";
btnTag.textContent = "light mode";
btnTag.style.color = "white";
}
}
document.body.appendChild(btnTag);
};
changeLight();
记住
- 作业的
=
==
用于按值进行比较
- 用于按类型和值进行比较的
=
使用切换
。if语句只有一个=
,用于赋值而不是计算它们。这应该只是添加和删除一个类。