我有以下CSS类:
.test{
background:red;
position:absolute;
display:flex;
color: white;
}
我们是否可以将.test类的内容存储在JS变量中?像这样的东西:
myTestClass = "{background:red;position:absolute;display:flex;color: white;}";
我可以想象,如果css位于样式标记中,它会起作用。
类似的东西
document.querySelector("style").textContent.match('.test {(.*?)}')
但是我不知道如何从css文件中提取css。我的意思是。。。如果您想在那里存储css的实际内容。对我来说,还不完全清楚你想完成
只需使用window.getComputedStyle
const test = document.getElementById('test')
const style = window.getComputedStyle(test)
const myTestClass = {
background:style.backgroundColor
}