0

2つのボタンがあるナビゲーションがあり、ボタンをクリックすると新しいページが読み込まれ(新しいページの読み込み)、ボタンをクリックすると、クラス名が選択されたクラスが追加されます...部分的に実行されます-クラスが追加されますが、新しいページをロードすると、クラスは再び削除されますか?ユーザーがクリックしたものにクラスを固定するにはどうすればよいですか?

$(document).on("click", ".list-mode button", function () {
    $(".list-mode button").removeClass("selected");
    $(this).addClass("selected");
});
4

2 に答える 2

2

If you're navigating to a new page, changes you've made using JavaScript to the current page aren't going to appear in the new one. You really have three options:

  1. Add the correct class to the correct element in the HTML of the new page using server-side code.
  2. Add the correct class to the correct element using JavaScript in the new page; you'll need a way to determine which element the class should be added to (possibly part of the URL of the new page).
  3. Use AJAX navigation, and rather than loading a completely new page simply load the new content and replace part of the already loaded page.
于 2013-02-18T09:18:59.373 に答える
0

これには Cookie または DOM ストレージが必要です。

ページが放棄されるときはいつでも、ユーザーが最後に行ったことをどこかに保存し、保存された設定でボタン全体を初期化する必要があります。

jQuery を使用しているのでdocument.ready、ページが読み込まれるたびにイベントを処理し、そこでボタンを初期化します。

$(document).ready(function() {
   // Initialize the whole buttons
});
于 2013-02-18T09:20:14.247 に答える