【发布时间】:2021-08-19 14:33:05
【问题描述】:
我想从 html 中移除 style 属性:style="">
例如:
<html style="--athens-gray:#121212; --alabaster:#1E1E1E; --black:#ffffff; --white:#000000;">
应该是<html>。
这是我的脚本:
const themes = {
dark: {
'--white': '#000000',
},
light: {
'--white': '#ffffff',
},
};
function lighttheme() {
const theme = themes["dark"];
for (var variable in theme) {
document.documentElement.style.setProperty(variable, theme[variable]);
};
}
:root {
--athens-gray: #e9e8ec;
--alabaster: #f8f8f8;
--black: #000000;
--white: #ffffff;
}
body {
background-color: var(--white);
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Settings</title>
</head>
<body>
<button type="button" onClick="lighttheme();">Click Me!</button>
</body>
</html>
【问题讨论】:
-
删除是什么意思?
-
你可以去掉for循环,这样样式属性就不会被设置,样式属性也不会被添加。
-
我想更改主题,但是当我更改 更改为