2010-02-06 76 views

回答

7

为了得到(第一)的样式表对象使用

document.styleSheets[0] 

要访问的(第一个)规则在上的样式表使用:

document.styleSheets[0].cssRules[0] // firefox 
document.styleSheets[0].rules[0] // IE 

您可以

添加规则
insertRule(rule, index)     // firefox 
addRule(selector, declaration, [index]) // IE 

因此,做什么,你在Firefox描述:

document.styleSheets[0].insertRule("*{margin:0; padding:0;}", 0) 

而要做到这在IE:

document.styleSheets[0].addRule("*", "margin:0; padding:0;", 0) 

参见:Dom StyleSheet Object

+1

请参阅http://stackoverflow.com/questions/714655/how-can-i-set-a-css-hover-on-a-dom-created-element-in-javascript/714717#714717和http:// stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript/524721#524721对于动态创建样式表的两种不同方法,如果您不想追加到现有的样式表 – Christoph 2010-02-06 14:30:56

-1

如果您想更改body元素的padding和margin:

document.body.setAttribute('padding', '0'); 
document.body.setAttribute('margin', '0'); 
+0

这只会在身上设置样式,问题是在所有事物上设置样式 - * – meouw 2010-02-06 10:37:38

+0

实际上我想要改变整个文档的光标类型(不仅仅是在主体内)。 – 2010-02-06 10:39:58