簡體   English   中英

如何通過JavaScript更改文檔的樣式?

[英]How can I change the style of the document via JavaScript?

就像這樣* {margin:0; padding:0;}

要獲取(第一個)樣式表對象,請使用

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對象

如果要更改body元素的填充和邊距:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM