document.styleSheets[x].cssRules are null

你说的曾经没有我的故事 提交于 2019-12-10 20:56:02

问题


When I inspect a website,
I see the cssRules from document.styleSheets[x].cssRules

However, with this website stackoverflow.com, when I inspect with Chrome browser, I see document.styleSheets, but cssRules is null.

How come this is possible?


回答1:


That's because the style sheets are coming from a different domain. Some browsers (such as Chrome) implement strict cross-domain policies by throwing security errors or setting the cssRules and ownerRule to null when it comes from a different domain...in your case the style sheets come from a CDN

MDN quotes the following in the CSSStyleSheet documentation...

In some browsers, if a stylesheet is loaded from a different domain, calling cssRules results in SecurityError.

https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet



来源:https://stackoverflow.com/questions/46356349/document-stylesheetsx-cssrules-are-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!