Ajax调用加载当前内容

Ajax call loads the current content

本文关键字:调用 加载 Ajax      更新时间:2023-09-26

我浏览了很多论坛,绞尽脑汁想让这个超级简单的加载函数发挥作用。我已经将它缩小到我认为是loadContent函数的.fadeIn()部分的选择器问题。

我可以理解为什么它一直加载相同的东西,因为它使用相同的选择器,但基于我一直尝试使用的教程(http://css-tricks.com/rethinking-dynamic-page-replacing-content/)以及我开始工作的另一种(甚至更坏的)方法(基于此:http://code.tutsplus.com/tutorials/how-to-load-in-and-animate-content-with-jquery--net-26),它似乎应该工作!

我尝试过使用$(href)作为选择器,但它只是淡入旧内容,不加载任何内容。我还尝试添加一个警报来查看href是什么,它返回了正确的html页面,后面跟着正确的元素id(Articles/Cats.html#content)。

这是函数本身,如果你需要更多信息,我很乐意提供!任何帮助都会非常有帮助!

function articleClickLoad () {
    if (Modernizr.history) {
        var newHash      = "",
            $Content     = $("#content"),
            $el;
        $("#content").on("click", "a", function() {
            var _link = $(this).attr("href");
            var toLoad = $(this).attr('href')+' #content';
            history.pushState(null, null, _link);
            loadContent(toLoad);
            return false;
        });
        function loadContent(href){
            $Content.find("#content");
            $Content.fadeOut(1000, function() {
                $("main").load(href,'', function() {
                    $Content.fadeIn(1000);
                });
            });
        }
        $(window).bind('popstate', function(){
           _link = location.pathname.replace(/^.*['''/]/, ''); //get filename only
           loadContent(toLoad);
        });
    }
}
articleClickLoad();

谢谢!

我看到了两个潜在的问题。.load采用URL,但由于某种原因,您在单击处理程序中添加了' #content';它应该被删除Update:我忽略了传递页面片段是完全有效的,忘了这一点。

loadContent方法中,您在选择器$("main")上调用load。从你的来源来看,main可能是什么还不清楚,但我想应该是#content

还要注意,您可以从.load调用中删除第二个参数''。请参阅加载文档。