DocumentOrShadowRoot.styleSheets

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

styleSheets是DocumentOrShadowRoot接口定义的只读属性,它会返回一个StyleSheetListCSSStyleSheet 对象,这个对象对应的是通过引入或者嵌入文档中的样式表。

示例代码

function getStyleSheet(unique_title) {
  for (var i=0; i<document.styleSheets.length; i++) {
    var sheet = document.styleSheets[i];
    if (sheet.title == unique_title) {
      return sheet;
    }
  }
}

说明

返回的列表中,排序规则如下:

  • header标签中通过link标签引入的样式优先,多个link样式表以出现顺序排序
  • 接着是在文档中定义的样式表,存在多个这样的样式表时,以tree order(译注:先序深度优先遍历树)规则进行排序

规范

规范 状态 说明
Shadow DOM
DocumentOrShadowRoot
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
styleSheets
Experimental
Chrome Full support 53Edge Full support 12Firefox Full support 63IE Full support YesOpera Full support 40Safari Full support YesWebView Android Full support 53Chrome Android Full support 53Firefox Android Full support 63Opera Android Full support 41Safari iOS Full support YesSamsung Internet Android Full support 6.0

Legend

Full support  
Full support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.