javascript - 如何按正确的顺序选择多行表格标题中的表格单元格?

标签 javascript jquery html-table jquery-selectors

我有一个这样的标题:

<thead>
   <tr>
     <th rowspan="2">Cell-1</th>
     <th rowspan="2">Cell-2</th>        
     <th colspan="3">Header</th>
     <th rowspan="2">Cell-6</th>
   </tr>
   <tr>
     <th">Cell-3</th>
     <th">Cell-4</th>
     <th">Cell-5</th>
   </tr>
</thead>

我需要创建一个 jquery 选择器,其中的单元格按正确的顺序 1-2-3-4-5-6。现在我的选择器看起来像这样:

hdrCols = thead.find( "tr:first th[rowspan=2], TR:first TH[rowspan=2]").add( thead.find( "tr:last-child th, TR:last-child TH" ) );

这给了我 1-2-6-3-4-5 并破坏了我的布局。

有没有办法创建一个选择器,以正确的顺序获取单元格?我无法移动列,因此任何解决方案都必须包含这一点。

最佳答案

尝试简单地使用 JavaScript sort()功能。像这样:http://jsfiddle.net/6wYQt/

function sortHeaders(a, b) {
    var x = $(a).text().toLowerCase();
    var y = $(b).text().toLowerCase();

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

thead   = $('thead');
hdrCols = thead.find( "tr:first th[rowspan=2], TR:first TH[rowspan=2]").add( thead.find( "tr:last-child th, TR:last-child TH" ) );

hdrCols.sort(sortHeaders);

console.log(hdrCols);
​

关于javascript - 如何按正确的顺序选择多行表格标题中的表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452605/

相关文章:

jQuery 获取元素下方

JavaScript:setTimeOut 位于另一个 setTimeOut 中(嵌套 setTimeOut)以刺激 API 响应不起作用

javascript - jQuery 中的启动/更改具有相同的单选按钮设置功能

javascript - Sencha 触摸 2 : How to get the values from an HTML input field displayed inside a Panel?

html - 如何仅在 Textarea 上设置 jQuery 事件?

javascript - 无法通过 JQuery 更改 RadioButtonFor 选择

javascript - 使用 jquery 获取 optgroup 中选择选项的索引

javascript - 添加选项以在每个表格行中进行选择

表格边框的CSS重置

html - table-footer-group 和 table-header-group 不占用表格的宽度