如何让jquery与ajax一起工作?(复制)

时间:2022-08-25 17:26:20

This question already has an answer here:

这个问题已经有了答案:

Have the below code which loads a php page into a div, once this content is added the jquery on that php page doesnt seem to work. It only works if you open it directly (without ajax)

让下面的代码将一个php页面加载到一个div中,一旦该内容添加到php页面上的jquery似乎不能工作。只有直接打开它(不使用ajax)才有效

any ideas?

什么好主意吗?

AJAX CODE

AJAX代码

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

Jquery code on PHP page

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>

1 个解决方案

#1


3  

document.ready is not triggered when you load your PHP.

文档。在加载PHP时不会触发ready。

But the right way to have a script run after you load external content would be to use a callback function:

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

$('#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]] 
    }); 
});

#1


3  

document.ready is not triggered when you load your PHP.

文档。在加载PHP时不会触发ready。

But the right way to have a script run after you load external content would be to use a callback function:

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

$('#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]] 
    }); 
});