5

content/ ディレクトリにあるさまざまなページのコンテンツをロードするために jquery $.ajax を使用して Web サイトを開発しています。また、ハッシュタグを使用して、戻るボタンやリロードなどを有効にしています。

しかし。アクティブなアイテムを含むメイン メニューがあり、1 つのメイン メニュー アイテムの詳細なページ間を移動すると、それがアクティブになります。次に、連絡先などのメイン メニュー項目の 1 つをクリックします。そのため、連絡先メニュー項目がアクティブになります。また、ブラウザの戻るボタンをクリックしても、アクティブなメニュー項目は変わりません。

では、アクティブなメイン メニュー項目の状態を記憶する方法はありますか? メインメニューはコンテナページにあります。

ありがとうございました!

$(function(){

        $('.menu a')
            .bind('click',function(e) {
                e.preventDefault();
                $('#content').html('Извините, страница не существует или находится в разработке!');
                $('.menu a').each(function() {$(this).removeClass('activelink');});
                location.hash=$(this).attr('href').match(/(([a-z]*)\W)*/)[2];

                $(this).addClass('activelink');
                return false;
            });

        $('#content').on('click','a',function(e) {
            if (!($(this).hasClass('address') || $(this).hasClass('zoom'))){
            e.preventDefault();
            $('#content').html('Извините, страница не существует или находится в разработке!');
            location.hash=$(this).attr('href').match(/(([a-z]*)\W)*/)[2];
            return false;
            }
            else {
                if ($(this).hasClass('address')) {
                alert('Вы покидаете сайт The House Of Events. Возвращайтесь к нам еще!');
                }
            }
        });

    function hashChange(){
        var page=location.hash.slice(1);
        if (page!=""){
            $.ajax({
                    type: "GET",
                    url: "content/" + page + ".html #sub-content",
                    success: function(data, status) {
                        $('#content').html(data);
                        },
                    error: function fail() {
                        $('#content').html('Error');
                        }
                });
        }
    }

    if ("onhashchange" in window){
        $(window).on('hashchange',hashChange).trigger('hashchange');
    } else { 
        var lastHash='';
        setInterval(function(){
            if (lastHash!=location.hash)
                hashChange();
            lastHash=location.hash;
        },100);
    }
});
4

1 に答える 1

0

ハッシュタグをつければちょっとした仕掛けができます。

これらを編集します。

$('#content').on('click','a',function(e) {
  if(!($(this).hasClass('address') || $(this).hasClass('zoom'))){
    e.preventDefault();
    $('#content').html('Извините, страница не существует или находится в разработке!');
    var page=location.hash.slice(1).split('/')[0] + '/' + $(this).attr('href').match(/(([a-z]*)\W)*/)[2];
    location.hash=page;
    return false;
  } else {
    if ($(this).hasClass('address')) {
      alert('Вы покидаете сайт The House Of Events. Возвращайтесь к нам еще!');
    }
  }
});

function hashChange(){
  var page=location.hash.slice(1).split('/')[1];
  if(page==='undefined'){
    page=location.hash.slice(1).split('/')[0];
  }
  if(page!=""){
    $.ajax({
      type: "GET",
      url: "content/" + page + ".html #sub-content",
      success: function(data, status) { $('#content').html(data); },
      error: function fail() { $('#content').html('Error'); }
    });
  }
}

HTML5 history API を使用することもできます。

これをクリックイベントの下に置きます(場所のハッシュを削除します)

var active=$('.menu a.active').attr('id'); // or var active=$('.menu a.active').text();
history.pushState({menu: active}, "Our events", "events.html");

次に、戻るボタンを使用して内容を変更しますonpopstate

window.onpopstate = function(e){
  if(e.state){
    $('.menu a.activelink).removeClass('activelink')
    $('.menu #'+JSON.stringify(e.state.menu).replace(/"/gi,'')).addClass('activelink');
    var url=window.location.pathname;
    $.ajax({ ... })
  }
}

そして、メニュー項目に多くのクラスを追加してそれらをチェックする場合、愚かな方法が必要です.hasClass()

于 2013-06-09T13:26:43.207 に答える