简体   繁体   English

未捕获的 DOMException:无法读取“cssRules”属性

[英]Uncaught DOMException: Failed to read the 'cssRules' property

I wonder why does my script work on firefox but not on google chrome我想知道为什么我的脚本可以在 Firefox 上运行,而不能在 google chrome 上运行

JS: JS:

var _timelineWidth = (Number.parseInt(document.styleSheets[0].cssRules[16].style.width) / 100) * document.body.clientWidth;

CSS: CSS:

#timeline {
  position: relative;
  top: 15px;
  left: 12.5%;
  height: 5px;
  background: #aaa;
  border-radius: 2.5px;
  cursor: pointer;
}

here's the error code from chrome这是 chrome 的错误代码

Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules未捕获的 DOMException:无法从“CSSStyleSheet”读取“cssRules”属性:无法访问规则

In latest Chrome, CORS security rules are applicable for style-sheets also (Similar to Iframe rules).在最新的 Chrome 中, CORS安全规则也适用于样式表(类似于 Iframe 规则)。

You can load and render them but, cannot access the content through javascript (If loaded from Cross-Domain ).您可以加载和呈现它们,但无法通过 javascript 访问内容(如果从 Cross-Domain 加载)。

If your CSS Stylesheet is from Same domain as of HTML /or included in same HTML file, you will be able to access document.styleSheets[elem].cssRules otherwise it will throw error如果您的 CSS 样式表来自与 HTML 相同的域/或包含在同一 HTML 文件中,您将能够访问document.styleSheets[elem].cssRules否则会抛出错误

Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

Cross Domain Stylesheet跨域样式表

跨域样式表

Same Domain Stylesheet相同域样式表

相同域样式表

You can add crossorigin="anonymous" to prevent the error.您可以添加crossorigin="anonymous"以防止错误。

<link rel="stylesheet" href="https://..." crossorigin="anonymous">

More info here:更多信息在这里:

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin

This will create a potencial cors request but the server must respond with Access-Control-Allow-Origin: * or Access-Control-Allow-Origin: <authorized-domain> .这将创建一个潜在的 cors 请求,但服务器必须响应Access-Control-Allow-Origin: *Access-Control-Allow-Origin: <authorized-domain>

You can check here to see how to add CORS support to your server.您可以在此处查看如何向您的服务器添加 CORS 支持。

For more information about CORS you can read this and this .有关 CORS 的更多信息,您可以阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的DOMException:无法从“ CSSStyleSheet”读取“ cssRules”属性 - Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet' Javascript:DOMException:无法从“CSSStyleSheet”读取“cssRules”属性:无法访问规则 - Javascript: DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules 未捕获的 DOMException:无法从“CSSStyleSheet”读取“规则”属性 - Uncaught DOMException: Failed to read the 'rules' property from 'CSSStyleSheet' 未捕获的DOMException:无法读取“ contentDocument” - Uncaught DOMException: Failed to read the 'contentDocument' Iframe chrome:未捕获的 DOMException:无法从“窗口”读取“localStorage”属性:拒绝访问此文档 - Iframe chrome : Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document 未捕获的 DOMException:无法从“IDBRequest”读取“结果”属性:请求尚未完成 - Uncaught DOMException: Failed to read the 'result' property from 'IDBRequest': The request has not finished 未捕获的 DOMException:无法使用 JQuery 设置“innerHTML”属性 - Uncaught DOMException: Failed to set the 'innerHTML' property using JQuery 未捕获的 DOMException:无法在“FileReader”上执行“readAsDataURL” - Uncaught DOMException: Failed to execute 'readAsDataURL' on 'FileReader' 未捕获的 DOMException:无法在“历史”上执行“pushState”: - Uncaught DOMException: Failed to execute 'pushState' on 'History': 未捕获的 DOMException:无法在“节点”上执行“appendChild” - Uncaught DOMException: Failed to execute 'appendChild' on 'Node'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM