【发布时间】:2019-09-23 09:46:35
【问题描述】:
单击时,我想更改“样式”头中的内部样式表。 (我不想以内联样式更改它!!)我设法将新的 divone css 添加为文本,但我无法删除旧的(element.classList.remove ("# divone");)。有没有 idlist.remove? ...,或任何其他方式来实现这一点。有什么想法或建议吗? 谢谢
<!DOCTYPE html>
<html>
<head>
<style id="style">
#divone{
width: 500px;
height: 50px;
background-color: red;
}
#divtwo{
width: 400px;
height: 80px;
background-color: purple;
}
</style>
</head>
<div id="divone"></div>
<div id="divtwo"></div>
<button onclick="myFunctionTest()">Click me</button>
function myFunctionTest() {
var element = document.getElementById("style");
element.classList.remove("#divone");
document.getElementById("style").textContent += "#divone{width: 400px; height: 35px; background-color: red;}";
}
【问题讨论】:
-
只需将新样式属性添加到相同样式标签,旧样式减速将被覆盖。为什么要删除到旧版本?
标签: javascript css onclick styles internals