gpt4 book ai didi

jquery - 我可以从 jquery 单击事件循环事件调用吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:51:58 24 4
gpt4 key购买 nike

我正在尝试使用 jquery 将行突出显示添加到表格行。我想要 3 种预定义的颜色,这些颜色使用一次单击事件进行循环。例如,如果用户第一次单击以黄色突出显示的行,再次单击会将其变为橙色,第三次单击将其变为红色。

我目前只能使用一种颜色(开/关)来做到这一点

当前代码:

var row_highlight_color = localStorage.getItem('row_highlight_color');  

if (!row_highlight_color) {
row_highlight_color = '#f89406';
}

// lets get our custom color definition and append it to the style sheet

$('<style>.row_highlight_css { background-color: '+row_highlight_color+' !important; color: #ffffff;}</style>').appendTo('head');

$('table.table-striped tbody tr').on('click', function () {
$(this).find('td').toggleClass('row_highlight_css');
});

关于如何实现这一点有什么想法吗?

最佳答案

我建议使用 data 元素来存储当前状态:

$('table.table-striped tbody tr').on('click', function () {

var $this = $(this);
var col = $this.data('state'); // get current state

if (col === undefined) {
col = 0; // pick the colour to use on first click
} else {
$this.removeClass('row_highlight_' + col); // remove previous class
col = (col + 1) % 3; // update state
}

$this.addClass('row_highlight_' + col) // add new class
.data('state', col); // update state
});

注意:这将独立维护每一行的状态。

关于jquery - 我可以从 jquery 单击事件循环事件调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com