我试图对tr
进行排序,但到目前为止我没有运气。
这是我的tr
结构。
<tr>
<td>
<a href="./BlueSky-TexasHealthResources/index.php" >Blue Sky-Texas</a>
</td>
<td>
<a href="./BlueSky-TexasHealthResources/view.php">View Data</a>
</td>
<td id="blue_sky_texas">
</td>
</tr>
<tr>
<td id = 'bj'>
<a href="./BountyJobs/index.php" >Bounty Jobs</a>
</td>
<td>
<a href="./BountyJobs/view.php">View Data</a>
</td>
</tr>
这是我迄今为止尝试过的Javascript。
<script type="text/javascript">
var $tr = $("tr");
$(document).ready(function () {
var alphabeticallyOrderedTr = $tr.sort(function (a, b) {
return $(a).find("a:first").text().toLowerCase().localeCompare($(b).find("a:first").text().toLowerCase());
});
$("#container").html(alphabeticallyOrderedTr);
});
</script>
答案 0 :(得分:1)
.sort()
是Array.prototyope
方法,而不是jQuery方法。尝试在调用.sort(function(){})
之前添加.get()
或.toArray()
;例如,$tr.get().sort(
$(document).ready(function() {
var $tr = $("tr");
var alphabeticallyOrderedTr = $tr.get().sort(function(a, b) {
return $(a).find("a:first").text().toLowerCase().localeCompare($(b).find("a:first").text().toLowerCase());
});
$("#container").append(alphabeticallyOrderedTr);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="container">
<tr>
<td>
<a>Y Jobs</a>
</td>
</tr>
<tr>
<td>
<a>Z Jobs</a>
</td>
</tr>
<tr>
<td id='bj'>
<a href="./BountyJobs/index.php">Bounty Jobs</a>
</td>
<td>
<a href="./BountyJobs/view.php">View Data</a>
</td>
</tr>
<tr>
<td>
<a>X Jobs</a>
</td>
</tr>
<tr>
<td>
<a href="./BlueSky-TexasHealthResources/index.php">Blue Sky-Texas</a>
</td>
<td>
<a href="./BlueSky-TexasHealthResources/view.php">View Data</a>
</td>
<td id="blue_sky_texas">
</td>
</tr>
</table>
&#13;