js动态修改html、body的css属性

977 阅读1分钟

js获取html标签

document.documentElement

js获取body标签

document.body

以react为例,当页面属性(status)改变时,动态改变html和body的背景色,代码如下

useEffect(() => {
    switch (status) {
        case 'a':
            document.body.style.background = 'color1' // 修改body的背景色为color1
            document.documentElement.style.background = 'color1' // 修改html的背景色为color1
            break
	case 'b':
            document.body.style.background = 'color2'
            document.documentElement.style.background = 'color2' 
            break
        default:
            break
	}
}, [status])