Ajax 链接在 ajax 加载的内容中


Ajax links inside ajax loaded content

我在获取 Ajax 加载链接以加载其他 Ajax 内容时遇到问题。基本上这是我的ajax代码:

$(function () {
var api = $("#content").jScrollPane().data('jsp');
    var reinitialiseScrollPane = function()
    {
        api.reinitialise();
    }
// attaching click handler to links
$("#contentcontainer a[href]").click(function (e) {
    // cancel the default behaviour
    e.preventDefault();
    // get the address of the link
    var href = $(this).attr('href');
    // getting the desired element for working with it later
    var $wrap = $('#content');
    $wrap
        // removing old data
        api.getContentPane()
        // load the remote page
        .load(href, reinitialiseScrollPane , function (){
        }
    );
});
});

基本上导航内的链接工作正常,因为它们是在加载页面时加载的,但是 ajax 内容中的链接(应该在导航链接加载内容的同一位置加载页面)不起作用,我的理解是需要某种".live"函数调用,因为一旦 ajax 加载内容,js 就不会重新扫描代码。

找到了一些解决方案,但没有一个可以与我使用的代码相关联。代码的第一部分不是 ajax,但对于滚动条插件,我没有删除它,因为 id 喜欢避免它被一个 dosent 保持计数的解决方案所作废。

谢谢。

在附加点击处理程序时尝试使用 .on() 方法(请参阅 jQuery 文档):

$(document).on('click', '#contentcontainer a[href]', function (e) {
    // Rest of your code here
});