使用JavaScript获取元素的自定义css属性(-mystyle) [英] Get element's custom css properties (-mystyle) using JavaScript

查看:372
本文介绍了使用JavaScript获取元素的自定义css属性(-mystyle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些元素具有自定义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屋!

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