0

ページからページへの ajax 呼び出しを実行するスクリプトを使用していますが、理解できないのは、href をクリックするたびにブラウザの現在の場所が www.blabla/#index に変わる理由です。以下のコードを見てください。ターゲットは、現在の場所を「#」のないものに変更することです -www.blabla/index

$("document").ready(function() {

var hash = window.location.hash.substr(1);
var href = $('#tabs-bar li a').each(function(){
    var href = $(this).attr('href');
    if(hash==href.substr(0,href.length-5)){
        var toLoad = hash+'.html #content';
        $('#content').load(toLoad)
    }                                           
});

$('#tabs-bar li a').click(function(){

    var toLoad = $(this).attr('href')+' #content';
    /// $('#content').fadeOut('100');
    $('#content').empty();
    $('#load').remove();
    $('#profile-cv').addClass("loading");
    //$('#content').append('<div class="loading"></div>');

    setTimeout(function () { loadContent(); }, 1000);

    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);

    function loadContent() {
        $('#profile-cv').load(toLoad,showNewContent())
    }
    function showNewContent() {
        $('#profile-cv').append(hideLoader());
    }
    function hideLoader() {
        $('#profile-cv').removeClass("loading");

    }
    return false;

});

});
4

2 に答える 2

0

イベントのデフォルト アクションの実行を停止する必要があります。

e.preventDefault();これを達成するために使用します -eはコールバック関数の最初の引数です:

$('#tabs-bar li a').click(function(e) {
    e.preventDefault();
    var toLoad = $(this).attr('href') + ' #content';
    ...
});
于 2012-11-01T11:31:32.010 に答える
0

設定しています

window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);

location.hashは常に「#」を追加します。

PJAX ライブラリを見て、URL を変更できるようにします。

アップデート

ライブラリは新しい HTML5 history.pushStateAPI を使用します。残念ながら、IE 10 以外の IE では動作しません。

この素敵なチュートリアルは、それを実装するのに役立ちます。

古いブラウザーのフォールバック ソリューションが必要な場合は、PJAXhistory.jsなどのライブラリを使用できます。

于 2012-11-01T12:14:56.823 に答える