insertRule不插入规则,但不给出任何错误

问题描述

我正在尝试将样式规则直接插入文档的开头。

它插入样式元素,但不插入代码本身。没有任何错误

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

I also asked this question on FCC but have no answers

解决方法

插入的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>