日期:2014-05-17  浏览次数:20694 次

关于jQuery的一个例子
本帖最后由 Javkburd 于 2013-05-09 21:11:06 编辑
我想在table表里选出颜色为dodgerblue的单元格,代码为:
$("#tableEmail td[background-color = dodgerblue]");
但是就是没有结果。其它的几个属性也用了,如:bgColor,background和backgroundColor,但还是不行,不知道为什么,用filter或not也不行。谁知道怎么写?程序逻辑应该没问题的。另外说一下上面几个背景色的应用场合和区别,我觉得还是不要混用为好。

------解决方案--------------------
background-color为rgb格式,不是dodgerblue
建议你使用class设置背景颜色
------解决方案--------------------
本帖最后由 net_lover 于 2013-05-09 22:15:16 编辑
你可以这样
var x = $('#tableEmail td').filter(function() {
    return $(this).css('background-color') == 'rgb(30, 144, 255)';
})
x.html("xxxxxxx")


x就是那个td

IE中这样写
$('#tableEmail td').filter(function() {
return $(this).css('background-color') == 'dodgerblue';
})

例子

<html>
<head>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>
<table id="tableEmail">
<tr>
<td style="background: dodgerblue">sdsd</td>
<td style="background: red">sdsd</td>
</tr>
</table>
<input type=button value=test onclick="xx()">
<script>
function xx() {
alert($('#tableEmail td').css("background-color"))
$('#tableEmail td').filter(function() {
return $(this).css('background-color') == 'dodgerblue';
}).html("xxxxxxx")

}
</script>
</body>
</html>

------解决方案--------------------
有2个方法
1,使用Color.js,下载使用方法参见name2rgb http://matthewbj.github.io/Colors/
2,使用json预定义
http://stackoverflow.com/questions/1573053/javascript-function-to-convert-color-names-to-hex-codes