简体   繁体   English

jQuery在指定一个后更改所有tr类

[英]jQuery change all tr classes after specified one

I have a data table with alternating row background colors. 我有一个具有交替行背景颜色的数据表。 I have an AJAX script to delete a row. 我有一个AJAX脚本来删除一行。 I can't come up with a way to change the class of all the rows beneath the one that was deleted so that it alternates correctly again. 我无法提出一种方法来更改已删除行下面的所有行的类,以使其再次正确交替。

For example, considering the following: 例如,考虑以下因素:

`<tr id="1" class="row1">
   <td>blah</td>
 </tr>
 <tr id="2" class="row2">
   <td>blah</td>
 </tr>
 <tr id="3" class="row1">
   <td>blah</td>
 </tr>
 <tr id="4" class="row2">
   <td>blah</td>
 </tr>`

Now, using my AJAX script, I remove id2, then id3 will move underneath id1 and they will have the same row color. 现在,使用我的AJAX脚本,删除id2,然后id3将移动到id1下,并且它们将具有相同的行颜色。 I managed to make my script change the next tr class, but that doesn't really help because then it's just the same color as the one after that. 我设法使脚本更改了下一个tr类,但这并没有真正的帮助,因为它的颜色与之后的颜色相同。 I can't figure out how to iterate through all of the next tr's, and change their class accordingly. 我无法弄清楚如何遍历所有下一个tr,并相应地更改其类。

What I have so far: 到目前为止,我有:

$('#news_' + id).fadeOut('slow');

var currtr = $('#news_' + id).attr('class');
var nexttr = $('#news_' + id).closest('tr').next('tr').attr('id');

$('#' + nexttr).removeClass($('#' + nexttr).attr('class'));
$('#' + nexttr).addClass(currtr);

You could just iterate over the visible <tr> elements, and remove the class from the even ones, and apply to the odd ones. 您可以只遍历可见的 <tr>元素,并从偶数元素中删除该类,然后将其应用于奇数元素。

Something like this: 像这样:

Example: http://jsfiddle.net/2CZdT/ 示例: http //jsfiddle.net/2CZdT/

$('tr:odd').addClass('odd');

$('td').click(function() {
    $(this).parent().fadeOut(function() {
        $(this).siblings('tr:visible').filter(':even').removeClass('odd')
            .end().filter(':odd').addClass('odd');
    });
});​

I have the click event on the <td> , so when one is clicked, it traverses up to the parent <tr> element, fades it out, the in the callback, it grabs all visible sibling <tr> elements, filters the even ones, removes the .odd class, then goes back and filters the odd ones, and adds the .odd class. 我在<td>上有click事件,因此当单击一个事件时,它会遍历到父<tr>元素,将其淡出,在回调中,它将捕获所有可见的同级<tr>元素,过滤偶数,删除.odd类,然后返回并过滤奇数,然后添加.odd类。

Note that this presumes there's a default class applied in your CSS, then you override the odd ones (or even ones) with the alternating class. 请注意,这假定您的CSS中应用了默认类,然后用交替类覆盖了奇数(或偶数)类。

Easiest way is to go over the whole table again, eg add this after the fadeOut: 最简单的方法是再次遍历整个表格,例如,在fadeOut之后添加以下内容:

$('#id_of_your_table tr:even').addClass('even');

Edit : on second thought, that won't work since the row you faded still exists, but just isn't visible. 编辑 :再三考虑,由于您褪色的行仍然存在,因此不起作用,但是不可见。 You need to remove it from the DOM, or skip it when re-applying the zebra-effect. 您需要将其从DOM中删除,或者在重新应用斑马效果时将其跳过。 Example: 例:

$('#news_' + id)
  .fadeOut('slow')
  .remove()
  .closest('table')
  .find('tr:even').addClass('even');

Or: 要么:

$('#news_' + id)
  .fadeOut('slow')
  .addClass('skip')
  .closest('table')
  .find('tr:not(.skip):even').addClass('even');

You can also target the table directly as in the first example, but you might as well move up from the faded row to the table its in. 您也可以像在第一个示例中一样直接定位表,但是您也可以从褪色的行向上移动到表所在的位置。

You could use the next siblings selector to get all the rows following the one you are going to delete. 您可以使用下一个兄弟姐妹选择器来获取将要删除的行之后的所有行。 Delete the desired row. 删除所需的行。 Then, you should already have the following siblings, so just .each() them and change their class. 然后,您应该已经具有以下兄弟姐妹,因此只需.each()它们并更改其类。

Eg 例如

var followingRows = $("#id2 ~ tr");
$("#id2").remove();
followingRows.each(function() {
    if (this.is('.even')
        this.removeClass('even').addClass('odd');
    else
        this.removeClass('odd').addClass('even');
});

Something close to that... 接近那个...

Let CSS do the work for you. 让CSS为您完成工作。

table tr:nth-child(2n+1) {
   background-color: #eef;
}

no JavaScript required! 无需JavaScript! =) =)

I would do something like this: 我会做这样的事情:

$('news_' + id).fadeOut('slow', function() {
    $(this).remove();
});
var i = 1;
$('tr').removeClass().each(function() {
    if (i == 1) {
        $(this).addClass('row' + i);
        i++;
    } else {
        $(this).addClass('row' + i);
        i--;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM