开发者

How can I change the style of the document via JavaScript?

That would go like this * {margin:0; paddin开发者_高级运维g:0;} in CSS.


To get the (first) stylesheet object use

document.styleSheets[0]

To access the (first) rule in the stylesheet use on of:

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

You can add rules with

insertRule(rule, index)                 // firefox
addRule(selector, declaration, [index]) // IE

Thus, to do what you describe in firefox:

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

And to do it in IE:

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

See also: Dom StyleSheet Object.


If you'd like to change the padding and margin for the body element:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜