<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
#hello{
color: #FF0000;
}
-->
</style>
</head>
<body>
<a href="#" id="hello">标签</a>
<script language="javascript">
<!--
document.getElementById("hello").onclick = function(){
alert(document.getElementById("hello").style.color);
}
//虽有在CSS中定义a标签的color样式,为什么用js取不出来相应的值??
-->
</script>
</body>
</html>
以上代码,虽有在CSS中定义a标签的color样式,为什么用js取不出来相应的值??
如果在js中有定义,是可以弹出对话框的,为什么,请大家帮忙?感谢你们。

解决方案 »

  1.   


    表于:2009-10-14 23:53:19<!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=gb2312" /> 
    <title>无标题文档 </title> 
    <style type="text/css"> 
    <!-- 
    #hello{ 
    color: #FF0000; 

    --> 
    </style> 
    </head> 
    <body> 
    <a href="#" id="hello">标签 </a> 
    <script language="javascript"> 
    <!-- 
    function CurrentStyle(element){
       return element.currentStyle || document.defaultView.getComputedStyle(element, null);
    };
    document.getElementById("hello").onclick = function(){ 
    alert(CurrentStyle(document.getElementById("hello")).color); 

    //虽有在CSS中定义a标签的color样式,为什么用js取不出来相应的值?? 
    --> 
    </script> 
    </body> 
    </html>
      

  2.   

    style只能拿行内样式~·如<div style="color:red;">
      

  3.   

    mubeibei,IE和FF输出的颜色值表示方法不一样,能不能都让它输出十六进制的颜色值?
      

  4.   


    alert(CurrentStyle(document.getElementById("hello")).color.toString(16)); 
      

  5.   

    alert(CurrentStyle(document.getElementById("hello")).color.toString(16)); 
    改成这句后FF中输出的还是rgb(255, 0, 0),而IE里是#999999啊,mubeibei,不好意思,可不可以再问问你,怎么回事儿,是我操作问题吗?
      

  6.   


    再转换一下就好了,判断一下如果是非IE浏览器的话,就把rgb(255,0,0)转成#FF0000的形式就搞定了,下面是转换过程的示例函数:function toHexColor(rgbColor) {
        var re = /^rgb/i,
            reExtract = /^rgb\((\d+),(\d+),(\d+)\)/gi;
        var hexRGB = "#", temp;
        if(re.test(rgbColor)) {
            reExtract.exec(rgbColor);
            temp = parseInt(RegExp.$1);
            hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
            temp = parseInt(RegExp.$2);
            hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16);
            temp = parseInt(RegExp.$3);
            hexRGB += temp < 16? "0" + temp.toString(16): temp.toString(16); 
            rgbColor = hexRGB;
        }
        return rgbColor;
    }alert(toHexColor("rgb(255,10,0)"));
      

  7.   

    默认情况下alert(document.getElementById("hello").style.color); 
    只能取到内嵌样式,如果你没有在HTML中使用style设置,
    那么就为空。