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

php中文网
发布: 2016-06-21 09:43:21
原创
1034人浏览过

本帖最后由 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即我们看到的下一个单元格了。

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号