Javascript 单击“更改页面背景色”页面
点击按钮后,我试图将页面背景颜色更改为“黑色”,将卡片内的文本颜色更改为“白色”。单击按钮时,颜色仅更改1秒并返回默认颜色 在下面的代码中,将向您展示一些来自主 HTML代码:Javascript 单击“更改页面背景色”页面,javascript,Javascript,点击按钮后,我试图将页面背景颜色更改为“黑色”,将卡片内的文本颜色更改为“白色”。单击按钮时,颜色仅更改1秒并返回默认颜色 在下面的代码中,将向您展示一些来自主 HTML代码: <div class="card fontOne"> <div> <hr /> </div> <ul class="card-items item-1"> <p class="name"&
<div class="card fontOne">
<div>
<hr />
</div>
<ul class="card-items item-1">
<p class="name">Roboto</p>
<button type="submit" class="font-button"><i class="material-
icons"></i></button>
</ul>
<p class="font-creator">Christina Robertson (12 styles)</p>
<p class="font-text cardParagraph">Then came the night of
the first falling star.</p>
</div>
<button class="btn-black" id="bMode" onclick="blackMode()"><i class="far fa-circle bg-black"></i></button>
我写的代码只是为了改变背景颜色,我不知道该怎么做。因此,我尝试只更改1段文字的颜色,但这也不起作用。
let时间戳;
const changeToBlack=document.getElementById('bMode');
const text=document.getElementById('font-text');
const oriBodyBgColor=document.body.style.backgroundColor;
const oriFontColor=text.style.color;
changeToBlack.addEventListener('单击',函数()){
if(时间戳){
clearTimeout(时间戳)
}
document.body.style.backgroundColor=“黑色”;
text.style.color=“白色”;
timeStamp=setTimeout(函数(){
document.body.style.backgroundColor=oriBodyBgColor;
text.style.color=oriFontColor;
}, 1000)
});代码>
机器人
;
克里斯蒂娜·罗伯逊(12种风格)
然后是第一颗流星的夜晚
点我
请提供帮助,谢谢。谢谢回复。但是我希望背景颜色改变,保持黑色,我不希望颜色消失。点击按钮后,它必须保持黑色。所以我认为设置超时不应该在那里。很抱歉,您可能不理解第一篇文章中的问题。
const changeToBlack = document.getElementById('bMode');
const text = document.getElementsByClassName('font-text');
changeToBlack.addEventListener('click', blackMode);
function blackMode() {
document.body.style.backgroundColor = "black";
text.style.color = "white";
}