选tr偶数行,JQuery< tr>条纹奇数/偶数行

quick JQuery question, I've got a products page that the user can filter. Each time a filter is applied/removed, a change event calls stripeTable() . I tried to implement the table striping using the following function, however after removing an item and calling stripTable(), the striping did not remain consistent, i.e each visible odd row one colour, each visible even row another.

function stripeTable() {

// Find all odd visible table rows and add .odd class.

$("#resultsTable > tbody > tr:even:visible").each(function() {

$(this).addClass('even');

});

// Find all even visible table rows and add .even class.

$("#resultsTable > tbody > tr:odd:visible").each(function() {

$(this).addClass('odd');

});

}

I cannot work out why the above wouldn't work. I managed to implement the function as below and it works fine. Any ideas?

function stripeTable() {

var count = 1;

// get all visible table rows

$("#resultsTable > tbody > tr").each(function () {

// If table row is visible, strip accordingly.

// Row 0 (table headers) not striped.

if ($(this).is(":visible") && (this.rowIndex !== 0)) {

if ((count % 2) != 0) {

// Remove class .even if applied previously

$(this).removeClass("even");

// Odd row, add class .odd

$(this).addClass("odd");

count++;

} else {

// Remove class .odd if applied previously

$(this).removeClass("odd");

// Even row, add class .even

$(this).addClass("even");

count++;

}

}

});

}

For clarity, stripeTable() is the last function called, the table rows are hidden/shown in the table before hand. Thanks.

解决方案

The order of the psuedo-selectors matters.

// Computes "even" first, then "visible"

"#resultsTable > tbody > tr:even:visible"

// Compute "visible" first, then "even"

"#resultsTable > tbody > tr:visible:even"

为了满足您的需求,我们可以基于提供的HTML结构,并结合jQuery编写相应的脚本来完成操作。以下是具体的实现步骤以及代码示例: ### 1. **点击thead标签中的总复框** - 当用户点击 `thead` 中的总复框时,所有位于 `tbody` 的分复框将被设置为相同的中状态。 ### 2. **点击tbody标签中的任意分复框** - 检查是否所有的分复框都处于中状态: - 若全是中,则自动勾总复框; - 否则取消总复框的择。 下面是完整的 jQuery 实现代码: ```html <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <table border="1"> <thead> <tr&gt; <th><input type="checkbox" id="selectAll"> 全</th> <th>序号</th> </tr&gt; </thead> <tbody> <tr&gt; <td><input type="checkbox" class="itemCheckbox"></td> <td>1</td> </tr&gt; <tr&gt; <td><input type="checkbox" class="itemCheckbox"></td> <td>2</td> </tr&gt; <tr&gt; <td><input type="checkbox" class="itemCheckbox"></td> <td>3</td> </tr&gt; </tbody> </table> <script> $(function () { // 绑定 "全" 功能到 #selectAll checkbox 上 $('#selectAll').on('click', function () { $('.itemCheckbox').prop('checked', this.checked); }); // 监听 tbody 内每一个 itemCheckbox 状态变化事件 $('tbody').on('click', '.itemCheckbox', function () { const allChecked = $('.itemCheckbox:checked').length === $('.itemCheckbox').length; // 设置 header 中 selectAll 的状态为 true/false if (allChecked) { $('#selectAll').prop('checked', true); // 所有都被中了 } else { $('#selectAll').prop('checked', false); // 至少有一个没被中 } }); }); </script> ``` --- ### 解释: #### (1)绑定全逻辑: 我们通过监听 `#selectAll` 的 `click` 时间,在每次触发该事件的时候将其中与否的状态同步给 `.itemCheckbox` 类的所有项。 ```javascript $('#selectAll').on('click', function () { $('.itemCheckbox').prop('checked', this.checked); }); ``` #### (2)监控子项的变化并更新父级项: 对于每个 `.itemCheckbox` 单独的操作情况我们也进了监视。若当前页面上所有的 `.itemCheckbox` 都被中,那么就将 `#selectAll` 自动打钩;只要存在一个没有被打钩的情况,就需要把 `#selectAll` 取消掉。 ```javascript $('tbody').on('click', '.itemCheckbox', function () { const allChecked = $('.itemCheckbox:checked').length === $('.itemCheckbox').length; if (allChecked){ $('#selectAll').prop('checked', true); }else{ $('#selectAll').prop('checked', false); } }); ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值