iterate style element to get css rules string

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent cf210efb56
commit 29f2e42978
4 changed files with 57 additions and 6 deletions

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>dynamic stylesheet</title>
<style></style>
<script>
const styleEl = document.querySelector('style');
const rules = [`body { margin: 0 }`, `p { background: lightpink }`];
rules.forEach((rule, idx) => {
styleEl.sheet.insertRule(rule, idx);
});
</script>
</head>
<body>
<p>p tag</p>
</body>
</html>