//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")
</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>
<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>
你应该跟“#FF0000”比较·判断是否相等
区分下浏览器行吗 ?
alert("true");
}
分别处理 。
http://jsfiddle.net/wPBvg/
把颜色的表达转到同一的样式
这个页面打不开,哈哈
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")