gpt4 book ai didi

css - 如何从 <style> 获取 CSSStyleSheet 对象

转载 作者:行者123 更新时间:2023-11-28 11:42:20 26 4
gpt4 key购买 nike

一个<style>对应一个CSSStyleSheet对象(在document.styleSheets中),但是怎么找,比如CSSStyleSheet对象对应<style id="-yCoder-styles">...</style>

console.log(document.styleSheets.length);

var style = document.createElement('style');
style.title = "-yCoder-styles";
style.type= 'text/css';
style.id = "-yCoder-styles";
document.head.appendChild(style);

//how to get the corresponding CSSStyleSheet instance from document.styleSheets

console.log(document.styleSheets.length);

var styleSheet = document.styleSheets[document.styleSheets.length - 1];
console.log(styleSheet.title == style.title);

最佳答案

根据您提供的代码,您应该能够通过以下方式获取关联的 CSSStyleSheet:

var styleSheet = style.sheet

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

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

关于css - 如何从 &lt;style&gt; 获取 CSSStyleSheet 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073998/

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