尝试读取 styleSheets 返回未定义

发布于 2024-11-07 02:11:50 字数 650 浏览 3 评论 0 原文

我已经寻找了答案,但似乎没有解决方案可以解决这个问题,所以希望具体说明它能帮助我找到解决方案。

我尝试使用 document.styleSheets[0].cssText 读取第一个样式表的 cssText,但它总是返回 undefined。我确实有一个样式表,而且它非常明显,但 JavaScript 似乎无法识别它。

然而,当我使用

感谢您的任何帮助。

编辑:这适用于 anydocument.styleSheets[0] 的方法,包括那些应该在多个浏览器上工作并且在使用 <链接>。例如:document.styleSheets[0].cssRules[0].selectorText

I've searched for an answer and no solution seems to fix this problem, so hopefully stating it specifically will help me find a solution.

I'm trying to read cssText of the first stylesheet using document.styleSheets[0].cssText, but it always returns undefined. I do have a stylesheet and it's visibly noticeable, but JavaScript doesn't seem to recognize it.

This did, however, work when I included the stylesheet within the page using <style>. I don't see why this would change when using <link>, though. Not only is it placed before the script, but it even returns undefined when using javascript:alert(document.styleSheets[0].cssText) in the omnibar after the page is fully loaded.

Thanks for any help.

Edit: This applies to any method with document.styleSheets[0], including those of which are supposed to work on multiple browsers and have worked for me prior to using <link>. For example: document.styleSheets[0].cssRules[0].selectorText

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ人海孤独症 2024-11-14 02:11:50

根据 quirksmode.orgdocument.styleSheets[n].cssText 仅在 IE 中受支持。

该表单

document.styleSheets[n].cssRules[m].cssText

似乎得到了更广泛的支持,因此您可以循环遍历该表单并根据各个规则构建一个字符串。 (尽管对于 IE,您必须将 cssRules 替换为 rules)。

According to quirksmode.org, document.styleSheets[n].cssText is only supported in IE.

The form

document.styleSheets[n].cssRules[m].cssText

seems to be more widely supported, so you could just loop over that and build a string from the individual rules. (Although you have to replace cssRules with rules for IE).

ι不睡觉的鱼゛ 2024-11-14 02:11:50

我认为这个答案已经过时了,但对其他人可能有用。
由于 2 个不同的原因,结果可能是“未定义”:

a) 在某些浏览器中,属性“cssRules”不起作用(根据 http://www.javascriptkit.com/domref/cssrule.shtml,仅在 NS/ Firefox 中受支持)。对于其他浏览器,您应该使用属性“rules”。

您可以使用以下方法解决问题:

if (document.styleSheets[0].cssRules)
    crossrule=document.styleSheets[0].cssRules[0]
else if (document.styleSheets[0].rules)
    crossrule=document.styleSheets[0].rules[0]

提取规则,然后提取选择器的名称:

document.styleSheets[0].cssRules[0].selectorText

b) 超越问题 a) 后,您可能会遇到另一个问题: 以 @ 开头的规则,例如@import of @font-face,视为规则。然而,他们的“selectorText”是未定义的。所以,你必须有办法跳过它们。我现在正在努力。但我还没有找到解决办法。无论如何,了解它的发生方式是有帮助的。

希望这有帮助

I thing that this answer is out of time, but it could be useful for someone else.
The result can be "undefined" due 2 different reasons:

a) In some browsers, the property "cssRules" does not work (in accord with http://www.javascriptkit.com/domref/cssrule.shtml, only for supported in NS/ Firefox). For the other browser, you should use the property "rules", instead.

You can solve the problem using something like:

if (document.styleSheets[0].cssRules)
    crossrule=document.styleSheets[0].cssRules[0]
else if (document.styleSheets[0].rules)
    crossrule=document.styleSheets[0].rules[0]

to extract the rule and, afterwards, to extract the name of selector:

document.styleSheets[0].cssRules[0].selectorText

b) After the overpassing of the issue a), you may have another problem: The rules that starts with a @, like @import of @font-face, as considered as rules. However, their "selectorText" is undefined. So, you must have a way of skip them. I am working on it, right now. But I have not found yet a solution. Anyway, it is an help to know way it is happening.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文