问题
I'm trying to use jquery tablesorter on tables that are loaded dynamically. But, while table sorter works fine in a static table, I can't get it to work in a loaded table.
I've tried calling tablesorter() after the load() and also calling it in the callback function of the load(), according to an answer to a similar question posted here. But none of them works.
This is an example of what I'm doing:
<html><head></head>
<body>
<!-- First table gets sorted properly -->
<table id="firstTable">
<thead><tr><th>Name</th><th>Number</th></tr></thead>
<tbody><tr><td>John</td><td>100</td></tr><tr><td>Paul</td><td>200</td></tr></tbody>
</table>
<!-- Second table where the sort doesn't work -->
<div id="secondTable"></div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.tablesorter.min.js"></script>
<script>
$(function($){
$("#firstTable").tablesorter();
$("#secondTable").load("secondtable.html", callback);
function callback() {
$("#secondTable").tablesorter();
}
});
</script>
</body></html>
And also the contents of 'secondtable.html':
<table id="secondTable">
<thead><tr><th>Name</th><th>Number</th></tr></thead>
<tbody><tr><td>Steve</td><td>500</td></tr><tr><td>Alice</td><td>800</td></tr></tbody>
</table>
来源:https://stackoverflow.com/questions/53213774/jquery-tablesorter-not-working-on-table-loaded-using-jquery-load