jQuery表格颜色:奇偶显示,鼠标悬停,点击固定颜色

本文介绍了一个用于网页中表格颜色动态设置的方法,通过JavaScript实现表头、偶数行、奇数行的不同颜色显示,并支持鼠标悬停及点击效果,增强了用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//表格颜色处理:-------------------------------------------------START
/*
说明:
$(function() {
_tableColorDecorator.initTableColor(tblIdArr, colorArr, captionRowspan, enableClickEvent, clickCount, clickColor);
});

tblIdArr: 要加色的table的id数组["table_id1", "table_id2"]
colorArr: 颜色数组[],依次是:表头颜色,偶数行,奇数行,mouseover,传""将使用默认颜色
captionRowspan:表头的行数,
enableClickEvent: 是否开启点击事件, 默认关闭
clickCount:传 1/2/"", 单击,双击,不启用
clickColor:颜色值,“”时候用默认颜色
*/
var _tableColorDecorator = {
captionColor: "#6492DD",
evenColor: "#F11FFF",
oddColor: "#aaFFFF",
mouseoverColor: "#FF1111",
clickColor: "FFFF11",
tmpColor: "#FFFFFF",
$clickedTr: null,
tmpClickColor: "#FFFFFF",
enableClickEvent: false,
clickCount: 1,

setCaptionTrColor: function($tbl, captionRowspan) {
$tbl.find("tr:lt(" + (captionRowspan) + ")").css("backgroundColor", _tableColorDecorator.captionColor);
},

setContentTrColor: function($tbl, captionRowspan) {
$tbl.find("tr:gt(" + (captionRowspan - 1) + "):even").css("backgroundColor", _tableColorDecorator.evenColor);
$tbl.find("tr:gt(" + (captionRowspan - 1) + "):odd").css("backgroundColor", _tableColorDecorator.oddColor);
},

bindEventHandler: function($tbl, captionRowspan) {
var $contentTrs = $tbl.find("tr:gt(" + (captionRowspan - 1) + ")");

$contentTrs.bind("mouseover", function() {
if(_tableColorDecorator.$clickedTr == null ||
(_tableColorDecorator.$clickedTr && _tableColorDecorator.$clickedTr.get(0).rowIndex != this.rowIndex)) {
_tableColorDecorator.tmpColor = $(this).css("backgroundColor");
$(this).css("backgroundColor", _tableColorDecorator.mouseoverColor);
}
});

$contentTrs.bind("mouseout", function() {
if(_tableColorDecorator.$clickedTr == null ||
(_tableColorDecorator.$clickedTr && _tableColorDecorator.$clickedTr.get(0).rowIndex != this.rowIndex)) {
$(this).css("backgroundColor", _tableColorDecorator.tmpColor);
}
});

if(_tableColorDecorator.enableClickEvent) {
var tmpEventName = _tableColorDecorator.clickCount == 1 ? "click" : "dblclick";
$contentTrs.bind(tmpEventName, function() {
if(_tableColorDecorator.$clickedTr == null) {
_tableColorDecorator.tmpClickColor = _tableColorDecorator.tmpColor;
$(this).css("backgroundColor", _tableColorDecorator.clickColor);
_tableColorDecorator.$clickedTr = $(this);
} else {
if(_tableColorDecorator.$clickedTr.get(0).rowIndex == this.rowIndex) {
$(this).css("backgroundColor", _tableColorDecorator.tmpClickColor);
_tableColorDecorator.$clickedTr = null;
} else {
_tableColorDecorator.$clickedTr.css("backgroundColor", _tableColorDecorator.tmpClickColor);
_tableColorDecorator.tmpClickColor = _tableColorDecorator.tmpColor;
$(this).css("backgroundColor", _tableColorDecorator.clickColor);
_tableColorDecorator.$clickedTr = $(this);
}
}
});

}
},

initVar: function(colorArr, enableClickEvent, clickCount, clickColor){
if(colorArr && colorArr.length == 4) {
_tableColorDecorator.captionColor = colorArr[0];
_tableColorDecorator.evenColor = colorArr[1];
_tableColorDecorator.oddColor = colorArr[2];
_tableColorDecorator.mouseoverColor = colorArr[3];
}

if(enableClickEvent && enableClickEvent != "") _tableColorDecorator.enableClickEvent = enableClickEvent;
if(clickCount && clickCount != "") _tableColorDecorator.clickCount = clickCount;
if(clickColor && clickColor != "") _tableColorDecorator.clickColor = clickColor;
},

initTableColor: function(tblIdArr, colorArr, captionRowspan, enableClickEvent, clickCount, clickColor) {
_tableColorDecorator.initVar(colorArr, enableClickEvent, clickCount, clickColor);

for(var i = 0; i < tblIdArr.length; i++) {
$tbl = $("#" + tblIdArr[i]);
if($tbl) {
_tableColorDecorator.setCaptionTrColor($tbl, captionRowspan);
_tableColorDecorator.setContentTrColor($tbl, captionRowspan);
_tableColorDecorator.bindEventHandler($tbl, captionRowspan);
}
}
}
}
//表格颜色处理:-------------------------------------------------END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值