1
如何保持hash
值在url
之內,並且即使在刷新頁面之後仍然保持標籤頁或div
的打開狀態。下面是我的示例代碼:在URL中使用哈希值重新加載頁面(F5)
$(document).ready(function() {
\t jQuery(function($) {
\t \t showTab(location.hash || "userView");
\t \t $("#nav a").click(function() {
\t \t \t var hash = this.getAttribute("href");
\t \t \t if (hash.substring(0, 1) === "#") {
\t \t \t \t hash = hash.substring(1);
\t \t \t }
\t \t \t window.location.hash = hash;
\t \t \t showTab(hash);
\t \t \t return false;
\t \t });
\t \t function showTab(hash) {
\t \t \t $(".displayUsers").hide();
\t \t \t switch (hash) {
\t \t \t \t case "userView": ViewUsers(); break;
\t \t \t \t case "userLogs": UserLogs(); break;
\t \t \t \t case "addUser": AddUser(); break;
\t \t \t }
\t \t }
\t });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
\t <dl id="nav">
\t \t <dt>
\t \t \t <p>•<a href="#addUser" class="listLink"> ADD USER</a></p>
\t \t </dt>
\t \t <dt>
\t \t \t <p>•<a href="#userLogs" class="listLink"> USER LOGS</a></p>
\t \t </dt>
\t \t <dt>
\t \t \t <p>•<a href="#userView" class="listLink"> VIEW USERS</a></p>
\t \t </dt>
\t </dl>
</div>
<div id="addUser" class="displayUsers" hidden></div>
<div id="userLogs" class="displayUsers" hidden></div>
<div id="userView" class="displayUsers" hidden></div>
刷新頁面後,hash
值被保存在url
但當前活動div
被設置爲默認值 - 隱藏。