用jQuery怎么得到<tr>对象的背景色,小弟急用,拜求!

解决方案 »

  1.   

    <script type="text/javascript" src="js/jq.js"></script>
    </head>
    <script type="text/javascript">
    <!--
    $(document).ready(function(){alert($("#kk tr:first").attr("bgcolor"));}
    )
    //-->
    </script>
    <body>
    <table border="1" id="kk">
    <tr bgcolor="#ff0000">
    <td>1</td>
    <td>2</td>
    </tr>
    <tr>
    <td>3</td>
    <td>4</td>
    </tr>
    </table>
    </body>
      

  2.   

    这样也可以
    <script type="text/javascript" src="js/jq.js"></script>
    </head>
    <script type="text/javascript">
    <!--
    $(document).ready(function(){alert($("#kk tr:first").css("backgroundColor"));}
    )
    //-->
    </script>
    <body>
    <table border="1" id="kk">
    <tr style="background-color:#ff0000">
    <td>1</td>
    <td>2</td>
    </tr>
    <tr>
    <td>3</td>
    <td>4</td>
    </tr>
    </table>
    </body>
      

  3.   

    是$("#kk tr:first").css("backgroundColor")还是$("#kk tr:first").css("background-color")
      

  4.   

    不过,运行之后怎么得到 rgb(255,0,0)
      

  5.   

    $("#kk tr:first").css("background-color")
      

  6.   

    而判断if($("#kk tr:first").css("background-color")=="rgb(255,0,0)")怎么返回false啊,
      

  7.   

    if($("#kk tr:first").css("background-color") 取到的颜色编码是大写“#FF0000”
    你应该跟“#FF0000”比较·判断是否相等
      

  8.   

    改成大写"#FF0000"还是不对,返回还是False
      

  9.   

    我试了是fire fox 才返回rgb什么的 。
    区分下浏览器行吗 ?
      

  10.   

    if($("#kk tr:first").css("backgroundColor") == "#ff0000"){
    alert("true");
    }
      

  11.   

    对,我用的是FF,那在FF应该怎么判断呢?
      

  12.   

    用window.navigator.userAgent.indexOf("Firefox")>=1
    分别处理 。
      

  13.   

    那在FF下判断$("#kk tr:first").css("backgroundColor") == "rgb(255,0,0)"吗?还是返回的False
      

  14.   

    楼主看看在IE,FF下值是多少
    http://jsfiddle.net/wPBvg/
      

  15.   

    顺便看看这个http://www.phpied.com/rgb-color-parser-in-javascript/
    把颜色的表达转到同一的样式
      

  16.   

    http://www.phpied.com/rgb-color-parser-in-javascript/
    这个页面打不开,哈哈
      

  17.   

    //Helper function to convert a digit to a two column Hex representation
    function toHex(N) {
    if (N==null) return "00";
    N=parseInt(N); if (N==0 || isNaN(N)) return "00";
    N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
    return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
    }
     
    //Function to convert rgb() format values into normal hex values
    function RGBtoHEX(str)
    {
    if (str.substring(0, 3) == 'rgb') {
    var arr = str.split(",");
    var r = arr[0].replace('rgb(','').trim(), g = arr[1].trim(), b = arr[2].replace(')','').trim();
    var hex = [
    toHex(r),
    toHex(g),
    toHex(b)
    ];
    return "#" + hex.join('');
    }
    else{
    return str;
    }
    }// 大写#FF0000
    alert(RGBtoHEX($("#kk tr:first").css("backgroundColor")) == "#FF0000")