文档:styleSheets 属性
styleSheets 是 接口的一个只读属性,它返回一个 Document 集合,其中包含 StyleSheetList 对象。这些样式表是显式链接到或嵌入在文档中的。CSSStyleSheet
值
返回的列表按以下顺序排序:
示例
通过标题检索特定样式表
js
function getStyleSheet(uniqueTitle) {
for (const sheet of document.styleSheets) {
if (sheet.title === uniqueTitle) {
return sheet;
}
}
}
访问样式表中的规则
您可以使用样式表、样式和 对象单独访问这些样式表及其规则,如下例所示,该示例会将所有样式规则选择器打印到控制台。CSSRule
js
for (const styleSheet of document.styleSheets) {
for (const rule of styleSheet.cssRules) {
console.log(`${rule.selectorText}\n`);
}
}
对于具有单个样式表的文档,其中定义了以下三个规则:
css
body {
background-color: darkblue;
}
p {
font-family: "Arial";
font-size: 10pt;
margin-left: 0.125in;
}
#lumpy {
display: none;
}
此脚本输出以下内容:
BODY P #LUMPY
规范
| 规范 |
|---|
| CSS 对象模型 (CSSOM) # dom-documentorshadowroot-stylesheets |
浏览器兼容性
加载中…