我可以通过Javascript获取非标准CSS属性的值吗? [英] Can I fetch the value of a non-standard CSS property via Javascript?

查看:190
本文介绍了我可以通过Javascript获取非标准CSS属性的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个自定义(非标准)CSS属性,在样式表(而不是内联样式属性)中设置并获取其值。以这个CSS为例:

I am trying to read a custom (non-standard) CSS property, set in a stylesheet (not the inline style attribute) and get its value. Take this CSS for example:

#someElement {
  foo: 'bar';
}



我设法使用IE7中的currentStyle属性获得其值:

I have managed to get its value with the currentStyle property in IE7:

var element = document.getElementById('someElement');
var val = element.currentStyle.foo;

但是currentStyle是MS特定的。所以我在Firefox 3和Safari 3中尝试了getComputedStyle():

But currentStyle is MS-specific. So I tried getComputedStyle() in Firefox 3 and Safari 3:

var val = getComputedStyle(element,null).foo;

...并返回undefined。 有没有人知道以跨浏览器的方式检索自定义CSS属性值?

...and it returns undefined. Does anyone know a cross-browser way of retreiving a custom CSS property value?

(您可能已经注意到, 't有效的CSS,但它应该工作,只要值遵循正确的语法。更好的属性名称将是-myNameSpace-foo或某事。)

(As you might have noticed, this isn't valid CSS. But it should work as long as the value follows the correct syntax. A better property name would be "-myNameSpace-foo" or something.)

推荐答案

Firefox不会从代码到DOM传递标签,属性或CSS样式。这是设计。 Javascript只能访问DOM,而不是代码。所以没有,没有办法访问javascript的浏览器本身不支持的属性。

Firefox does not carry over tags, attributes or CSS styles it does not understand from the code to the DOM. That is by design. Javascript only has access to the DOM, not the code. So no, there is no way to access a property from javascript that the browser itself does not support.

这篇关于我可以通过Javascript获取非标准CSS属性的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆