HTMLStyleElement: sheet 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLStyleElement 介面的一個只讀的 sheet 屬性,包含與該元素關聯的樣式表。

除非其 type 屬性不是 text/css,否則 StyleSheet 始終與 HTMLStyleElement 相關聯。

一個 StyleSheet 物件,如果與該元素沒有關聯,則為 null

示例

假設 <head> 包含以下內容

html
<style id="inline-style">
  p {
    color: blue;
  }
</style>

關聯的 HTMLStyleElement 物件的 sheet 屬性將返回描述它的 StyleSheet 物件。

js
const style = document.getElementById("inline-style");
console.log(style.sheet.cssRules[0].cssText); // 'p { color: blue; }'

規範

規範
CSS 物件模型 (CSSOM)
# dom-linkstyle-sheet

瀏覽器相容性