CSSRule: type property

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Thuộc tính chỉ đọc type của interface CSSRule là một thuộc tính lỗi thời, trả về một số nguyên cho biết CSSRule đại diện cho kiểu quy tắc nào.

Nếu bạn cần phân biệt các kiểu quy tắc CSS khác nhau, một lựa chọn thay thế phù hợp là dùng constructor.name:

js
const sheets = Array.from(document.styleSheets);
const rules = sheets.map((sheet) => Array.from(sheet.cssRules)).flat();

for (const rule of rules) {
  console.log(rule.constructor.name);
}

Giá trị

CSSRule.STYLE_RULE (1)

Quy tắc là CSSStyleRule, kiểu quy tắc phổ biến nhất: selector { prop1: val1; prop2: val2; }.

CSSRule.IMPORT_RULE (3)

Quy tắc là CSSImportRule và đại diện cho quy tắc @import.

CSSRule.MEDIA_RULE (4)

Quy tắc là CSSMediaRule.

CSSRule.FONT_FACE_RULE (5)

Quy tắc là CSSFontFaceRule.

CSSRule.PAGE_RULE (6)

Quy tắc là CSSPageRule.

CSSRule.KEYFRAMES_RULE (7)

Quy tắc là CSSKeyframesRule.

CSSRule.KEYFRAME_RULE (8)

Quy tắc là CSSKeyframeRule.

CSSRule.MARGIN_RULE (9)

Quy tắc là CSSMarginRule.

CSSRule.NAMESPACE_RULE (10)

Quy tắc là CSSNamespaceRule.

CSSRule.COUNTER_STYLE_RULE (11)

Quy tắc là CSSCounterStyleRule.

CSSRule.SUPPORTS_RULE (12)

Quy tắc là CSSSupportsRule.

CSSRule.FONT_FEATURE_VALUES_RULE (14)

Quy tắc là CSSFontFeatureValuesRule.

Các giá trị CSSRule.UNKNOWN_RULE (0), CSSRule.CHARSET_RULE (2), CSSRule.DOCUMENT_RULE (13), CSSRule.VIEWPORT_RULE (14), và CSSRule.REGION_STYLE_RULE (16) không còn lấy được nữa.

Ví dụ

js
const rules = document.styleSheets[0].cssRules;
console.log(rules[0].type);

Specifications

Thông số kỹ thuật
CSS Object Model (CSSOM)
# concept-css-rule-type

Browser compatibility