给你的风格标签一个ID,比如<style id="ssID">
如果有人正在为你制作你的风格
告诉那个人给样式标签一个 id -
这样你就可以直接访问它而无需
争先恐后地想知道它的索引是什么
// create a hash table
var cssHash = {};
// loop through and populate the hash table
for (let i in (r = ss0.sheet.rules)) {
// selectorText is the name of the rule - set the value equal to the rule
cssHash[r[i].selectorText] = r[i];
}
现在您有了样式表中所有内容的哈希表 -
请注意,有些值将是 undefined,但不是
你关心的任何事情
例如,如果您有一个名为 #menuItem 的类
并且您想将其颜色更改为黑色,请执行此操作
cssHash['#menuItem'].style.color = #000;
该行将设置规则样式的颜色
在哈希表(cssHash)中查找其索引
名为“#menuItem”
更重要的是,您可能有几种不同的
您想一次全部更改的课程
有点像你在大学转专业的时候
假设你有四个不同的类
你想设置他们所有的背景颜色
到相同的值,一些用户从输入中选择
颜色选择器标签是<input id="bColor" type="color">
并且您要更改的课程规则被称为
#menuItem .homeAddr span 和 #vacuum:hover
// create a listener for that color selector
bColor.addEventListener('input', function (e) {
// loop through a split list of the four class names
'#menuItem .homeAddr span #vacuum:hover'.split(' ').forEach(function (obj) {
// use the hash table to look up the index of each name
// and set the background color equal to the color input's value
cssHash[obj].style.backgroundColor = bColor.value;
});
}, false); // false added here for the sake of non-brevity