CSSGroupingRule: insertRule() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
Phương thức insertRule() của giao diện
CSSGroupingRule thêm quy tắc CSS mới vào danh sách các quy tắc CSS.
Cú pháp
js
insertRule(rule)
insertRule(rule, index)
Tham số
rule-
Một chuỗi
indexOptional-
Chỉ mục tùy chọn để chèn quy tắc; mặc định là 0.
Giá trị trả về
Chỉ mục của quy tắc mới.
Ngoại lệ
IndexSizeErrorDOMException-
Được ném ra nếu index lớn hơn số lượng quy tắc CSS con.
HierarchyRequestErrorDOMException-
Được ném ra nếu
rulekhông thể được chèn vào chỉ mục được chỉ định do một số ràng buộc CSS. HierarchyRequestErrorDOMException-
Được ném ra nếu
rulelà câu lệnh hợp lệ nhưng không phải là câu lệnh lồng nhau.
Ví dụ
js
let myRules = document.styleSheets[0].cssRules;
myRules[0].insertRule(
"html {background-color: blue;}",
0,
); /* inserts a rule for the HTML element at position 0 */
Thông số kỹ thuật
| Specification |
|---|
| CSS Object Model (CSSOM)> # dom-cssgroupingrule-insertrule> |