使用JavaScript获取元素的自定义css属性(-mystyle) [英] Get element's custom css properties (-mystyle) using JavaScript
问题描述
在某些元素具有自定义CSS属性的应用程序中,是否有通过JavaScript检索此类值的任何方法?
In an application where certain elements have custom CSS properties, is there any way to retrieve such a value via JavaScript?
例如
<div id="myDiv" style="color:#f00;-my-custom-property:upsidedown;" />
我可以通过以下两种方法访问color属性:
I can access the color attribute via these two methods:
$('myDiv').style.getPropertyValue("color")
$('myDiv').style.color
但这些不适用于自定义属性。
But these do not work for custom properties. Is this supported at all?
推荐答案
浏览器未使用的CSS值将被舍弃,无法通过 .style
然而,还有其他两个选项:
Use a custom attribute, as defined by HTML5. Then, simply retrieve that attribute's value. This is the recommended route.
使用HTML5定义的自定义属性。然后,只需检索该属性的值。这是建议的路线。
HTML
<div id="myDiv" style="color:#f00;" data-custom-property="upsidedown" />
HTML
< div id =myDivstyle =颜色:#f00; data-custom-property =upsidedown/>
这篇关于使用JavaScript获取元素的自定义css属性(-mystyle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!