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
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).
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.
发布评论
评论(2)
根据 quirksmode.org,
document.styleSheets[n].cssText 仅在 IE 中受支持。
该表单
似乎得到了更广泛的支持,因此您可以循环遍历该表单并根据各个规则构建一个字符串。 (尽管对于 IE,您必须将
cssRules
替换为rules
)。According to quirksmode.org,
document.styleSheets[n].cssText
is only supported in IE.The form
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
withrules
for IE).我认为这个答案已经过时了,但对其他人可能有用。
由于 2 个不同的原因,结果可能是“未定义”:
a) 在某些浏览器中,属性“cssRules”不起作用(根据 http://www.javascriptkit.com/domref/cssrule.shtml,仅在 NS/ Firefox 中受支持)。对于其他浏览器,您应该使用属性“rules”。
您可以使用以下方法解决问题:
提取规则,然后提取选择器的名称:
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:
to extract the rule and, afterwards, to extract the name of selector:
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