https://github.com/NV/auto_update_stylesheets
我正在尝试更新这个很棒的脚本以处理残酷的 css 导入,例如:
<style type="text/css" media="all">@import url("http://192.168.1.238/system.base.css");
</style>
该脚本仅适用于 css 的正常包含,例如:
<link href="stylesheets/bundle_github.css" media="screen" rel="stylesheet" type="text/css" />
我理解这个脚本的逻辑,但我的问题是如何选择 @import url ('foo')
,url()
中的部分,这个示例 foo
。如果我知道这是否可行,我就可以解决我的问题。
谢谢。
最佳答案
确实没有一种好的跨浏览器方法可以做到这一点。参见 QuirksMode有关浏览器实现 CSS 规则的方式的详细信息。
您必须确定获取 @import
规则的正确方法才能解析它。
对于 IE,您需要获取 document.styleSheets[0].imports[0]
假设第一个 style
或 link rel="stylesheet "
页面上的引用包含 @import
规则作为其第一条规则。看起来你需要调用 document.styleSheets[0].removeImport(0);
和 document.styleSheets[0].addImport("URL");
来修改。
其他浏览器似乎使用 document.styleSheets[0].cssRules[0]
假设相同的结构。对于那些你需要使用 document.styleSheets[0].deleteRule(0)
和 document.styleSheets[0].insertRule('@import url("URL")')
。这些是标准的 DOM Level 2 规则;见here .
希望对您有所帮助。
关于@import url() 的 jquery 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6192717/