首先,抱歉英语不好。
好吧,我需要使用 javascript 更改所有段落的颜色属性,这是我的 html 和 JS 代码:
<body>
<p>Parragraph one</p>
<p>Parragraph two</p>
<button onclick="CE()">change style</button>
</body>
for (var j=0;j<document.styleSheets[0].cssRules.length;j++) {
if(document.styleSheets[0].cssRules[j].selectorText='p')
document.styleSheets[0].cssRules[j].style.color="#FF0000";
}
这是我的 CSS 代码:
p{
color: green;
font-size: 20px;
font-weight: bold;
font-family: arial;
}
我也试着用这个来改变它:document.styleSheets[0].cssRules[0].style.backgroundColor="#FF0000"
;
(试图改变背景颜色,只是为了看看它是否有效,但只适用于 Mozilla Firefox)
最佳答案
用你想要的颜色创建另一个类,然后使用 jQuery 添加和删除你认为合适的类。
关于javascript - 使用 javascript 更改外部 CSS 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19786196/