简体   繁体   中英

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

That would go like this 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM