javascript - 如何使 jquery 与 ajax 一起工作?

标签 javascript php jquery html 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]] 
    }); 
});

关于javascript - 如何使 jquery 与 ajax 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481914/

相关文章:

javascript - 为什么动态地给新的 Image() 对象一个 src 有时会给出宽度和高度 0

javascript - .load 开始新路径

javascript - 如何将 URL trim/剥离为页面名称?

jquery - 如何更改asp.net中type属性的值?

jquery - 有条件地显示/隐藏 div

jquery - CSS - 覆盖宽度

javascript - 使用angularjs获取div的文本

PHP - 从时间戳获取毫秒

php - 使用 php 和 mysql 登录 android

php - 将数组转换为另一种格式