关于如何得到样式中 class的属性

浪漫小和 2012-09-21 10:45:59
情况如下
<button class="stately" id="stately" value="1" onclick="updateFlag(this);">牢固</button>

class="stately" 是我调用的一个style样式(html) ,这个样式里面有个属性color.

我现在遇到的问题就是 我希望在updateFlag(this) 这个js函数里面如何获取到这个样式的颜色的值?

谢谢!!
...全文
144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
</head>

<style type="text/css">
.background { background-color:Gray; width:150px; }
.word { color:red; font-size:12px; height:100px; }
</style>

<script type="text/javascript">

if (!window.getComputedStyle) {
window.getComputedStyle = function(element) {
if (!element) throw 'element is null or undefined';
if (!element.currentStyle) throw 'element unsupport currentStyle';
var cs = element.currentStyle;
!cs.getPropertyValue && (cs.getPropertyValue = function(css) {
return this[css];
});
return cs;
};
}

window.onload = function() {
var content = document.getElementById('content');
var color = window.getComputedStyle(content).getPropertyValue('background-color');
alert(color);
}

</script>

<body>
<div id="content" class="background word">test</div>
</body>
</html>
cscript 2012-09-21
  • 打赏
  • 举报
回复
你把你那个样式定义发上来撒

this.style.color 怎么取不到? 不可能,除非是没有
浪漫小和 2012-09-21
  • 打赏
  • 举报
回复
没 就是需要那个属性阿。。。
cscript 2012-09-21
  • 打赏
  • 举报
回复
额 是代码
浪漫小和 2012-09-21
  • 打赏
  • 举报
回复
什么代表???????
cscript 2012-09-21
  • 打赏
  • 举报
回复
你直接把你代表 搞上来吧 应该没什么问题的
浪漫小和 2012-09-21
  • 打赏
  • 举报
回复
我试过了 拿不到。。
cscript 2012-09-21
  • 打赏
  • 举报
回复
function updateFlag(obj){
var color = obj.style.color;
alert(color);
}
木薯超人 2012-09-21
  • 打赏
  • 举报
回复
用JQUERY
先下JQUERY.JS
var str_color = $('#stately').attr('color');
alert(str_color);
木薯超人 2012-09-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

你把你那个样式定义发上来撒

this.style.color 怎么取不到? 不可能,除非是没有
[/Quote]

用JQUERY
先下JQUERY.JS
var str_color = $('stately').attr('color');
alert(str_color);

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧