StyleSheetList

StyleSheetList interface represents a list of StyleSheet .

It is an array-like object but can't be iterated over using Array methods. However It can be iterated over in a standard for loop over its indices, or converted to an Array .

范例

Get document styleSheet objects with for loop

for (let i = 0; i < document.styleSheets.length; i++) {
  let styleSheet = document.styleSheets[i];
}
					

Get all CSS rules for the document using Array methods

const allCSS = [...document.styleSheets]
  .map(styleSheet => {
    try {
      return [...styleSheet.cssRules]
        .map(rule => rule.cssText)
        .join('');
    } catch (e) {
      console.log('Access to stylesheet %s is denied. Ignoring...', styleSheet.href);
    }
  })
  .filter(Boolean)
  .join('\n');
					

规范

规范 状态 Comment
CSS Object Model (CSSOM)
The definition of 'CSSStyleSheetList' in that specification.
工作草案

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
StyleSheetList Chrome 1 Edge ≤18 Firefox 31 IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 31 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
item Chrome 1 Edge 12 Firefox 31 IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 31 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
length Chrome 1 Edge 12 Firefox 31 IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 31 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持
完整支持
兼容性未知
兼容性未知