【发布时间】:2020-05-06 12:49:23
【问题描述】:
对于一个研究项目,我必须使用 JavaScript 创建一个动态表。我向一些朋友寻求帮助,但由于他们不是计算机科学家(或任何其他方向的人),我只知道使用 AJAX 和 DOM 很好。并使用以下 javascipt(src=w3schools 和堆栈)
问题 它正在排序,但不是字母或数字。它可能是表格一列中包含的图片中的 url,但对我来说它看起来是随机的。
如何创建动态工作排序表??
<table id="phones">
<thead>
<th><h2>Brand<br><p>Click to sort</p>
<p><button onclick="sortTable()"></button></p></h2></th>
<th><h2>IMG</h2></th>
<th><h2>Model<br><p>Click to sort</p>
<p><button onclick="sortTable()"></button></p></h2></th>
<th><h2>Os<br><p>Click to sort</p>
<p><button onclick="sortTable()"></button></p></h2></th>
<th><h2>Screensize<br><p>Click to sort</p>
<p><button onclick="sortTable()"></button></p></h2></th>
<th><h2>Price<br><p>Click to sort</p>
<p><button onclick="sortTable()"></button></p></h2></th>
</thead> ```
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById('phones');
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[0];
y = rows[i + 1].getElementsByTagName("td")[0];
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
【问题讨论】:
标签: javascript html dynamic tablesorter tablesort