本文介绍了按名称而不是索引获取document.styleSheets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与索引一起使用,

,但如果我想使用与特定的CSS文件?

document.styleSheets is used with an index,
but what If I want to use stylesheet.insertRule with a specific CSS file ?

我知道文件的名称,它被注入到一个页面,并在某些时候通过JS。

I know the file's name, which is injected to a page and at some point via JS.

推荐答案

使用这个,并牢记:



setStyleRule = function(selector, rule) {
    var stylesheet = document.styleSheets[(document.styleSheets.length - 1)];

    for( var i in document.styleSheets ){
        if( document.styleSheets[i].href && document.styleSheets[i].href.indexOf("myStyle.css") ) {
            stylesheet = document.styleSheets[i];
            break;
        }
    }

    if( stylesheet.addRule ){
        stylesheet.addRule(selector, rule);
    } else if( stylesheet.insertRule ){
        stylesheet.insertRule(selector + ' { ' + rule + ' }', stylesheet.cssRules.length);
    }
}

这篇关于按名称而不是索引获取document.styleSheets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:03
查看更多