jQuery: tablesorter not working on table loaded using jquery load()

半世苍凉 提交于 2019-12-11 06:12:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!