如何使jquery与ajax协同工作


how to make jquery work with ajax?

有下面的代码将php页面加载到div中,一旦添加了这些内容,该php页面上的jquery似乎就不起作用了。只有当您直接打开(不使用ajax)时,它才有效

有什么想法吗?

AJAX代码

<script>
   $(document).ready (function() {
      $('#NavButton1').click(function(event) {
        $('#content').empty();
        $('#content').load('placeholder.php');
      })
    })
</script>

PHP页面上的Jquery代码

<script>

$(document).ready(function() { 
    // call the tablesorter plugin 
    $('#myTable').tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[0,0],[2,0]] 
    }); 
}); 
</script>

document.ready在加载PHP时不会被触发。

但是,在加载外部内容后运行脚本的正确方法是使用回调函数:

$('#content').load('placeholder.php', function() { 
    // call the tablesorter plugin 
    $('#myTable').tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[0,0],[2,0]] 
    }); 
});