gpt4 book ai didi

javascript - 如何从 HTMLStyleElement 元素获取 CSSStyleSheet 对象

转载 作者:太空狗 更新时间:2023-10-29 14:19:58 25 4
gpt4 key购买 nike

是否可以从 HTMLStyleElement ( document.styleSheets[0] ) 中获取 CSSStyleSheet 对象 ( document.createElement('style') )?

我想将 css 规则动态添加到尚未插入文档中 <style>元素。

最佳答案

可以<style> 中获取 CSSStyleSheet 对象元素。

var style = document.createElement('style');
document.head.appendChild(style);
var styleSheet = style.sheet // Will give you the associated CSSStyleSheet

这仅在 之后 <style> 有效元素已附加到文档中。

没有特别好的文档记录,很难通过在控制台中四处查找来找到。

关于javascript - 如何从 HTMLStyleElement 元素获取 CSSStyleSheet 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com