1

我无法获取加载 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 内容内的链接(应该在导航链接加载内容的同一位置加载页面)不起作用,我的理解是需要一些一旦ajax加载内容,称为js的“.live”函数不会重新扫描代码。

我找到了一些解决方案,但没有一个与我使用的代码有关。代码的第一部分不是 ajax,但对于滚动条插件,我没有删除它,因为 id 喜欢避免它被不计入计数的解决方案取消。

谢谢。

4

1 回答 1

2

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

$(document).on('click', '#contentcontainer a[href]', function (e) {
    // Rest of your code here
});
于 2013-11-03T00:00:54.740 回答