我在获取 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,而是滚动条插件,我没有删除它,因为我想避免它被一个解决方案作废,该解决方案将其计入计数。
谢谢。
最佳答案
在附加点击处理程序时尝试使用 .on()
方法(参见 jQuery documentation ):
$(document).on('click', '#contentcontainer a[href]', function (e) {
// Rest of your code here
});
关于javascript - ajax 加载内容中的 Ajax 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19748289/