相关疑难解决方法(0)

pushState()和popState():操纵浏览器的历史记录

我正在开发一个小项目,我想在其中创建一个Ajax风格的网站.内容加载了jQuery load().正如你们中的一些人所知,这样做的缺点是URL不会相应地改变显示的内容.为了完成这项工作,你可以使用pushState().因为这不支持跨浏览器,所以我使用了插件history.js.

这很好用,但问题是我的后退和前进按钮不能正常工作,我不知道我做错了什么.URL正在正确更改,内容也是如此,但标题根本没有变化.标题我的意思是显示为浏览器窗口的窗口(或标签)的标题.即<title>加载的页面或引用该页面的链接的标题属性(它们是相同的).我认为这与事实,我只能拨打其标题我需要的页面的部分做(加入即#contentload()).我仍然想要那个页面的标题.这是我迄今为止所做的测试案例.(自2013年12月28日起不再提供.)jQuery文件:

$(document).ready(function () {
    var firstLink = $("ul > li:first-child > a");
    var menuLink = $("ul > li > a");
    var firstLoadedHtml = firstLink.attr("href") + " #content";

    $("#sectionContainer").hide().load(firstLoadedHtml).fadeIn("fast");
    firstLink.addClass("active");   

    menuLink.click(function(e) {
        history.pushState(null, null, this.href);

        e.preventDefault();
        e.stopImmediatePropagation();

        var CurLink = $(this);
        var newLoadedHtml = CurLink.attr("href") + " #content";
        var oldSection = $(".contentPage");

        $("#sectionContainer").hide().load(newLoadedHtml).fadeIn("fast");
        menuLink.removeClass("active");
        CurLink.addClass("active");
    });

    $(window).bind('popstate', function() {
            var returnLocation = history.location || document.location;

            $('#sectionContainer').load(returnLocation + "#content"); …
Run Code Online (Sandbox Code Playgroud)

jquery html5 history.js pushstate popstate

6
推荐指数
1
解决办法
5万
查看次数

标签 统计

history.js ×1

html5 ×1

jquery ×1

popstate ×1

pushstate ×1