我正在尝试为一个论坛做一个小的chrome扩展,但我只想让它在论坛的特定区域工作。
问题是,我不能只做“匹配”:“子论坛”,因为在该论坛的线程不能区分他们是在什么子论坛的网址。
该子论坛有自己的css样式表,所以我想如果我可以做一个快速检查,看看当前的样式表是否与subforum.css匹配,它会工作得很好。
基本上,我只需要检查样式表的名称,如果这是不可能的,我如何才能准确地检查当前样式表是否包含某个单词或元素等。
发布于 2012-08-26 09:51:41
使用Attribute Contains Selector
if ($('link[href*="myStylesheet.css"]').length) {
//stylesheet containing "myStylesheet.css" in the href attribute was found
} else {
//not found
}当然,如果给出了完整的确切属性值,也可以使用Attribute Equals Selector。
if ($('link[href="path/To/myStylesheet.css"]').length) {
//stylesheet with link="myStylesheet.css" found, do stuff
}发布于 2012-08-26 09:58:38
您可以遍历样式表本身和@import规则
function sheetExists(name){
var s = document.styleSheets, i = s.length, j = 0;
while(i-->0){
if(s[i].href !== null){
if(s[i].href.indexOf(name) !== -1) return true;
}
while(j < s[i].cssRules.length && s[i].cssRules[j].type === 3){
if(s[i].cssRules[j].href !== null){
if(s[i].cssRules[j].href.indexOf(name) !== -1) return true;
}
j++;
}
j = 0;
}
return false;
}
sheetExists('subforum.css');https://stackoverflow.com/questions/12127013
复制相似问题