html表格行颜色不同颜色,jquery怎么设置表格奇偶行不同的颜色?

在jQuery中使用“$("tr:odd").css("background-color","颜色")”与“$("#tr:even").css("background-color","颜色")”即可设置表格奇偶行颜色不同。

ba54dd33a485160cbcd9bb88c6edfe96.png

jquery实现表格奇偶行不同颜色的代码:

//用CSS控制奇偶行的颜色

$(document).ready(function(){

$("tr:odd").css("background-color", "#e6e6fa");

$("#tr:even").css("background-color", "#fff0fa");

}

:even 选择器选取每个带有偶数 index 值的元素(比如 2、4、6)。

:odd 选择器选取每个带有奇数 index 值的元素(比如 1、3、5)。

完整代码:

$(document).ready(function(){

$("tr:odd").css("background-color","#B2E0FF");

$("tr:even").css("background-color", "#fff0fa");

});

Welcome to My Web Page

IdLastNameFirstNameAddressCity
1AdamsJohnOxford StreetLondon
2BushGeorgeFifth AvenueNew York
3CarterThomasChangan StreetBeijing
4ObamaBarackPennsylvania AvenueWashington

效果如下:

579e79d8c42df11d96cfab029acd0b63.png

前端开发学习请关注HTML中文网

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 layui 的表格渲染功能,通过设置单元格的样式来实现表格某一的背景颜色。具体实现方式如下: 1. 在表格渲染之前,定义一个回调函数,用于设置单元格的样式。 ```javascript var table = layui.table; table.render({ elem: '#demo', cols: [[ //表头 {field: 'id', title: 'ID', width:80}, {field: 'username', title: '用户名', width:120}, {field: 'sex', title: '性别', width:80, sort: true}, {field: 'city', title: '城市', width:100}, {field: 'sign', title: '签名', width: 200}, {field: 'experience', title: '积分', width: 80, sort: true}, {field: 'score', title: '评分', width: 80, sort: true}, {field: 'classify', title: '职业', width: 100}, {field: 'wealth', title: '财富', width: 135, sort: true}, {fixed: 'right', title:'操作', toolbar: '#barDemo', width:150} ]], data: tableData, done: function(res, curr, count) { // 设置第二的背景颜色为 #F2F2F2 var trElem = $('.layui-table tbody tr:nth-child(2)'); trElem.css('background-color', '#F2F2F2'); } }); ``` 在上述代码中,通过 done 回调函数来实现对表格的单元格样式进设置。 2. 在回调函数中通过 jQuery 选择器选中表格中的某一,并设置单元格的背景颜色。 ```javascript var trElem = $('.layui-table tbody tr:nth-child(2)'); trElem.css('background-color', '#F2F2F2'); ``` 在上述代码中,通过 jQuery 选择器选中表格中的第二,并设置单元格的背景颜色为 #F2F2F2。 通过以上实现方式,您就可以在 layui 的表格设置某一的背景颜色了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值