我有一个包含选择标签的页面,该标签允许使用一些javascript“动态”更改页面的css 主题。
现在,我想允许更改页面中特定 html 元素的 css 样式。
例如,我有两个“主题”样式表:
/* in red theme file 'theme_red.css' */
.title{color:red;}
.text{...}
/* in blue theme file 'theme_blue.css' */
.title{color:blue;}
.text{...}
我在页面中有一些“选择”标签“更改主题”。这是一个极简的例子,在实际情况中,“主题”html 元素的数量和样式表的数量是模块化的。
我正在寻找创建一个像这样的javascript函数:(纯javascript ES6)
loadCssThemeFileOnTheFlyForSpecificHtmlElement(idOfSpecificHtmlElement, themeName){
// 1) Insert a new link tag in the head of the page with href attribute "themeName + '.css'"
...
// 2) for each css rules selector (of the css file ?), override the css selector rule like this :
// `.selector1` become `#idOfElement .selector1`
// `.selector2` become `#idOfElement .selector2`
// ...
// `.selectorN` become `#idOfElement .selectorN`
}
我陷入了函数的第2步insertCssFileOnTheFly。是否可以用一些 javascript 来做我想做的事情?我的意思是,是否可以循环当前页面的 css 规则并向每个选择器规则添加字符串“#idOfElement”?
炎炎设计
月关宝盒
相关分类