insertRule() 不插入规则但不给出任何错误
insertRule() not inserting rule but not giving any errors
我正在尝试将样式规则直接插入到文档的头部。
它插入样式元素而不是代码本身。没有错误或任何错误。
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(
"figure#styleThumbs { \
position: absolute; \
left: 0px; \
bottom: 0px; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img { \
outline: 1px solid black; \
cursor: pointer; \
opacity: 0.75; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img:hover { \
outline: 1px solid red; \
opacity: 1.0; \
} \
");
Picture of HTML Head with style but no style code
插入的 CSS 没有显示在 DOM 中,但确实有效。
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(`#test{
background-color: dodgerblue;
}`);
<div id="test">
This is a test
</div>
我正在尝试将样式规则直接插入到文档的头部。
它插入样式元素而不是代码本身。没有错误或任何错误。
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(
"figure#styleThumbs { \
position: absolute; \
left: 0px; \
bottom: 0px; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img { \
outline: 1px solid black; \
cursor: pointer; \
opacity: 0.75; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img:hover { \
outline: 1px solid red; \
opacity: 1.0; \
} \
");
Picture of HTML Head with style but no style code
插入的 CSS 没有显示在 DOM 中,但确实有效。
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(`#test{
background-color: dodgerblue;
}`);
<div id="test">
This is a test
</div>