javascript - 使用 Javascript 读取外部 css

标签 javascript css google-chrome

所以如果有一个外部 css 文件,例如:

<link rel='stylesheet' type='text/css' href='test.css'>  

我们想使用 javascript 读取样式表,我们可以这样做:

document.styleSheets[0].cssRules[0].cssText; (for example for first rule)

它在 firefox 中工作正常,但同样的语句在 chrome 中给出错误,类似 Type Error: "non_object_property_load"

可能是什么问题,我们如何阅读 chrome 中的样式表?

请看看我面临的问题:

http://s4.postimage.org/x5q0xt1vh/file.png  

编辑:所以当测试文件和链接的 css 放置在系统本地并且无法通过网络服务器访问时,问题就来了。

最佳答案

好的,我发现了问题。它确实与安全相关,如果外部 css 在 firefox 的同一目录中但不适用于 chrome,则该方法有效。
如果 css 文件在另一个目录或在线,它也不会在 firefox 中工作。

现在为了解决这个问题,我使用参数 --allow-file-access-from-files 启动了 chrome,现在它可以正常工作了。

关于javascript - 使用 Javascript 读取外部 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7968926/

相关文章:

jquery - 如何制作图像幻灯片循环的 Controller ?

javascript - 如何从选项卡外的链接打开 jquery 选项卡?

google-chrome - 0.0.0.0 :8000 working in Firefox, 但在 Chrome 中不存在

javascript - 中型数据发布有效,而大数据发布抛出错误? (Node.js 和 RabbitMQ)

jquery - 带有 flex-wrap 的 CSS flex 设计

javascript - TableViewSection.remove 示例

javascript - window.location.replace(href) 后返回按钮失效;

java - 在不同的显示器上测试失败 selenium webdriver java

javascript - 在 HTML 输入中显示文本

javascript - 使用 GetElementById 传递多个值(在特定字符后拆分)