Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有表,我需要按可用索引(仅 VanilaJS 而不是 jQuery)对第列进行排序(排序)。我该怎么做?请参阅示例http://jsfiddle.net/mcqueen/AXF2Y/3/。
我会这样做:
var positions = [1, 4, 2, 0, 5, 3], tr = document.getElementById('table-id').tHead.rows[0], cells = [].slice.call(tr.cells); positions.forEach(function(pos, i) { tr.insertBefore(cells[pos], tr.cells[i]); });
http://jsfiddle.net/ybr6E/
有助于从 NodeList 中创建一个数组,因为 NodeList 在您更改 DOM 时会更新。