如何使它在JavaScript中按下按钮时,整个背景变为黑色,文本变为白色?

如何使它在JavaScript中按下按钮时,整个背景变为黑色,文本变为白色?,javascript,html,Javascript,Html,我如何制作一个按钮,当我按下它时,它会将背景颜色从白色(原始)更改为黑色?(所有这些JavaScript代码都将在HTML中运行)我希望它看起来像这样: <button onclick = "changeToBlack()">Change Background To Black</button> function changeToBlack(){ /* in here i want it to change the color of the body

我如何制作一个按钮,当我按下它时,它会将背景颜色从白色(原始)更改为黑色?(所有这些JavaScript代码都将在HTML中运行)我希望它看起来像这样:

<button onclick = "changeToBlack()">Change Background To Black</button>

function changeToBlack(){
/*
in here i want it to change the color of the body
*/
}
将背景更改为黑色
函数changetBlack(){
/*
在这里,我想让它改变身体的颜色
*/
}

你应该用类和css来做,但如果你很懒,那么答案就在这里。(您需要将文本元素设为p)

将背景更改为黑色
函数changetBlack(){
document.body.style.background=“黑色”
document.querySelector(“p”).style.color=“白色”
}

感谢@px1mp的帮助!
<button onclick = "changeToBlack()">Change Background To Black</button>

function changeToBlack(){
document.body.style.background = "black"
document.querySelector("p").style.color = "white"
}