Javascript 获取表行对并使用JQuery进行排序

Javascript 获取表行对并使用JQuery进行排序,javascript,jquery,Javascript,Jquery,如何使用javascript或jquery将表中的行成对地放入变量中?之后,我想在桌子上做一个简单的排序 <table> <thead> <tr> <th rowspan="2">Visitor</th> <td>Scheduled In</td> <td>Time In</td> </tr> <t

如何使用javascript或jquery将表中的行成对地放入变量中?之后,我想在桌子上做一个简单的排序

<table>
 <thead>
    <tr>
        <th rowspan="2">Visitor</th>
        <td>Scheduled In</td>
        <td>Time In</td>
    </tr>
    <tr>
        <td>Scheduled Out</td>
        <td>Time Out</td>
    </tr>
</thead>
<tbody>
    <tr>
        <th rowspan="2">Santos Angelo Borodec</th>
        <td>9am</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>5pm</td>
        <td>&nbsp;</td>
    </tr>
  ...
  </tbody>
</table>
通过选择像
tr:nth child(4n)这样的行,tr:nth child(4n-1)
对我不起作用

有没有一个简单的方法来实现这一点

这基于“”中的排序代码


这是我的小提琴,它创建了一个boggle board:

试试这种方法,在
tr
对上进行排序

$('.sort-table').click(function (e) {
    var $sort = this;
    var $table = $('#sort-table');

   //Find the even rows and its next one, clone and wrap them into temp table.
    var $rows = $table.find('tbody > tr:even').map(function () {
        return $(this).next().andSelf().clone().wrapAll('<table />')
    }); 
  //Give each table which contains the pair to be sorted
    $rows.sort(function (a, b) {
        var keyA = $('th', a).text();
        var keyB = $('th', b).text();
        if ($($sort).hasClass('asc')) {
            return (keyA > keyB) ? 1 : 0;
        } else {
            return (keyA > keyB) ? 1 : 0;
        }
    });

    var tbody = $('tbody', $table).empty();//empty the tbody
    $.each($rows, function (index, row) {
        $(tbody).append($(row).unwrap());//Unwrap the table and get the rows alone.
    });
    e.preventDefault();
});
$('.sort table')。单击(函数(e){
var$sort=this;
var$table=$(“#排序表”);
//找到偶数行及其下一行,克隆并将它们包装到临时表中。
var$rows=$table.find('tbody>tr:even').map(函数(){
返回$(this.next().andSelf().clone().wrapAll(“”)
}); 
//给出包含要排序的对的每个表
$rows.sort(函数(a,b){
var keyA=$('th',a).text();
var keyB=$('th',b).text();
if($($sort).hasClass('asc')){
返回(keyA>keyB)?1:0;
}否则{
返回(keyA>keyB)?1:0;
}
});
var tbody=$('tbody',$table).empty();//清空tbody
$.each($行,函数(索引,行){
$(tbody.append($(row.unwrap());//打开表并单独获取行。
});
e、 预防默认值();
});

谢谢这是一个很好的解决方案,不需要对原始代码进行太多修改。
$('.sort-table').click(function (e) {
    var $sort = this;
    var $table = $('#sort-table');

   //Find the even rows and its next one, clone and wrap them into temp table.
    var $rows = $table.find('tbody > tr:even').map(function () {
        return $(this).next().andSelf().clone().wrapAll('<table />')
    }); 
  //Give each table which contains the pair to be sorted
    $rows.sort(function (a, b) {
        var keyA = $('th', a).text();
        var keyB = $('th', b).text();
        if ($($sort).hasClass('asc')) {
            return (keyA > keyB) ? 1 : 0;
        } else {
            return (keyA > keyB) ? 1 : 0;
        }
    });

    var tbody = $('tbody', $table).empty();//empty the tbody
    $.each($rows, function (index, row) {
        $(tbody).append($(row).unwrap());//Unwrap the table and get the rows alone.
    });
    e.preventDefault();
});