Heim  >  Artikel  >  Web-Frontend  >  关于jQuery的一个例子_html/css_WEB-ITnose

关于jQuery的一个例子_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:43:21878Durchsuche

本帖最后由 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>

你可以这样
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>

确实是的,我测试了一下,IE将background-color解析成颜色名,而Chrome, Firefox和Safari都解析成rgb值。我想问能有通过颜色名得到rgb的方法吗。还有就是我用
var select = td.filter(function () { return $(this).css("background-color") == "rgb(30, 144, 255)" }); 
select.next()得到下一个单元格,可就是为空,不知道怎么回事。

有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

有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
谢谢你,还有就是为什么select.next()得不到下一个单元格?


有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
谢谢你,还有就是为什么select.next()得不到下一个单元格?
我知道为什么了,因为各个td位于不同的tr中,并不是兄弟关系,而我的td都只有1个,所以select.next()为空。正确的做法是select.parent().next().children().first(),先得到父元素tr,然后得到下一个tr,再得到tr所有子元素td,最后得到第一个子元素td即我们看到的下一个单元格了。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn