【发布时间】:2020-03-06 09:30:18
【问题描述】:
我在我的 Wordpress 网站的自定义 javascript 中使用此功能。
var addRule = (function (sheet) {
if(!sheet) return;
return function (selector, styles) {
console.log(sheet.cssRules)
if (sheet.insertRule) return sheet.insertRule(selector + " {" + styles + "}", sheet.cssRules.length);
if (sheet.addRule) return sheet.addRule(selector, styles);
}
}(document.styleSheets[document.styleSheets.length - 1]));
这里的问题是上面的功能在主页上可以正常工作,但是,
在内页上抛出Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules。
这是我网站的链接,
Homepage//here you will see no error in console
Inner Page//here you will see an error in console
根据我的研究,此错误是由跨域资源共享 (CORS) 引起的, 但为什么它在主页上工作?
【问题讨论】:
标签: javascript css google-chrome dom