0

URL 値を入力してもタイトルが変わらず、最初のコンテンツが非表示にならない

<script type="text/javascript">
    $(function() {
    var pagecontent = $(".page_content");
    var pages = $("ul.pages li,#logo");
    var hash = window.location.hash;
    var title = document.title;

    pagecontent.not(hash).hide();
    pages.first().addClass("active").show();
    pagecontent.first().show();
    pages.find('[href=' + hash + ']').addClass('active');
    pages.find('[href=' + hash + ']').parent().closest('li').addClass('active');

    pages.click(function() {
        $(this).addClass('active').siblings().removeClass('active');
        $(this).parent().closest('li').addClass('active').siblings().removeClass('active'); 
        pagecontent.hide();
        var activepage = $(this).find("a").attr("href");

        $(activepage).fadeIn();
        title = $('h2', activepage).first().text();   
        $('title').text(title);
        return false;
    });
});
</script>

URL を入力しようとしても、タイトルと pagecontent.first は変更されません。

4

1 に答える 1

0

修理済み。ここにあります:

$(function() {
    var pagecontent = $(".page_content");
    var pages = $("ul.pages li,#logo");
    var hash = window.location.hash || "#home";
    var title = document.title;

    // new block
    var titlex = $('h2', hash).first().text();
    document.title = titlex;
    $(hash).show();

    $('ul.pages a[href="' + hash + '"]')
        .addClass("active")
        .closest("li")
            .addClass("active");
    // end

    pagecontent.not(hash).hide();
    pages.click(function() {
        $(this).addClass('active').siblings().removeClass('active');
        $(this).parent().closest('li').addClass('active').siblings().removeClass('active'); 
        pagecontent.hide();
        var activepage = $(this).find("a").attr("href");
        window.location.hash = activepage;  //new: fix location hash
        $(activepage).fadeIn();
        title = $('h2', activepage).first().text();   
         $('title').text(title);
        return false;
    });
});

スタイルについて話し合うこともできますが、これは機能します。

編集:clickハンドラーが適切な場所のハッシュを設定することに注意してください。正しい判断のように思えますが、ブラウザのタイトル バーがちらつきます。気になるなら交換してみる

window.location.hash = activepage;
$('title').text(title);

if( history.pushState ) {
    history.replaceState({}, title, activepage);
} else {
    window.location.hash = activepage;
    document.title = title;
}
于 2012-09-30T21:44:11.620 に答える