Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript访问CSS自定义变量_Javascript_Html_Css_Variables - Fatal编程技术网

使用Javascript访问CSS自定义变量

使用Javascript访问CSS自定义变量,javascript,html,css,variables,Javascript,Html,Css,Variables,我在css文件中有如下样式 #galleryImages{ position:absolute; top:24px; left:41px; width:900px; moving:false; } 当我试图通过Javascript访问它时,它返回未定义的 外部css是正确的,它正确地返回其他样式变量以及getElementById alert("External: " + document.styleSheets[0].cssRules[2].style

我在css文件中有如下样式

#galleryImages{
    position:absolute;
    top:24px;
    left:41px;
    width:900px;
    moving:false;
}
当我试图通过Javascript访问它时,它返回未定义的

外部css是正确的,它正确地返回其他样式变量以及getElementById

alert("External: " + document.styleSheets[0].cssRules[2].style.moving + 
    "\nInternal: " + document.getElementById("galleryImages").style.moving);
它会发出以下警告:

External: Undefined
Internal: Undefined
有没有办法通过javascript访问自定义CSS变量

提前感谢

大多数(所有?)浏览器不会将未知的CSS加载到DOM中,JavaScript不能直接访问CSS样式,只能访问加载到DOM中的样式

我能想到的唯一方法是实现您自己的CSS解析JavaScript,但对于mostpart来说,这对于您想要做的事情来说可能太过分了,而纯JavaScript解决方案或类值会更好

e、 g.在HTML中

<div class="moveable"></div>

使用该示例,您可以使用JavaScript获取元素的类名,如果它有“moveable”类,您就知道它可以移动

编辑:
@Anurag发布的Mozilla bug链接中提到,作为CSS 2.1规范的一部分,未知CSS属性将被忽略。

据我所知,浏览器完全忽略了非标准CSS定义,因此它不会出现在任何引用它的尝试中。我可能错了,但我很确定我没有错。您使用自定义css变量有什么原因吗?如果你的网站是HTML5,那么我会使用一个数据属性,这是为了解决这种情况而设计的,即使不是html5,我仍然认为数据属性是更好的解决方案。看看这个和这个mozilla。那个
moving
CSS属性做什么?