<!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=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.red{
background-color:#F00;
width:100px;
height:100px;
}
.green{
background-color:green;
width:50px;
height:50px;
}
.blue{
background-color:blue;
width:100px;
height:100px;
}
</style>
</head><body>
<div class="red">
<div class="green"></div>
</div>
<div class="blue"></div>
<script type="text/javascript">
function getColor(dom){
if(document.defaultView&&document.defaultView.getComputedStyle){
return document.defaultView.getComputedStyle(dom).backgroundColor;
}else{
return dom.currentStyle.backgroundColor;
}
}
document.body.onclick=function(e){
var a=e||window.event;
var src=a.srcElement||a.target;
alert(getColor(src));
}
</script>
</body>
</html>如果是想取元素字的颜色 不好判断点击的位置是否正好有字还是没字阿