我正在使用JQuery UI在我的應用程序中製作標籤。我需要可以鏈接的標籤(直接鏈接打開頁面並選擇正確的標籤)。這是通過使用散列標記/ fragmented identifier完成的。但是,當標籤上方和標籤內的內容很長時,我遇到了一個問題。使用URI哈希來識別標籤時防止滾動
單擊選項卡時,頁面向下滾動到選項卡的開頭。這不是我想要的。
我正在使用Jquery 1.7.1和JQuery UI 1.8.16。
javascript/jquery代碼是除了事件「tabsshow」之外的標準Jquery UI選項卡。這表明在Changing location.hash with jquery ui tabs(#1題)和JQuery UI Tabs: Updating URL with hash while clicking the tab(博客 - 由Robin科技日記)
$(document).ready(function() {
$("#tabs").tabs();
/**
* Add hash to URL of the current page
*
* http://chwang.blogspot.com/2010/02/jquery-ui-tabs-updating-url-with-hash.html
* https://stackoverflow.com/questions/570276/changing-location-hash-with-jquery-ui-tabs
*/
$("#tabs").bind('tabsshow',function(event, ui) {
window.location.hash = ui.tab.hash;
});
});
以下HTML可以用來測試行爲
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<div style="height: 400px;">Some other content</div>
<div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top"><a href="#tab_1"><span>Tab 1</span></a></li>
<li class="ui-state-default ui-corner-top"><a href="#tab_100"><span>Tab 100</span></a></li>
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tab_1000"><span>Tab 1000</span></a></li>
</ul>
<div id="tab_1" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
<table style="height: 1000px"><tr><td>Hello. This is tab 1</td></tr></table>
</div>
<div id="tab_100" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
<table style="height: 1000px"><tr><td>Hello. This is tab 100.</td></tr></table>
</div>
<div id="tab_1000" class="ui-tabs-panel ui-widget-content ui-corner-bottom"><h2>Heading</h2>
<table style="height: 1000px"><tr><td>Hello. This is tab 1000.</td></tr></table>
</div>
</div>
當下面打開的頁面URL,應該打開標籤1並且不向下滾動到標籤開始的位置。點擊其中一個標籤也是一樣。
file.html#tab_1
的[JQuery用戶界面選項卡從而造成屏幕「跳」]可能重複(http://stackoverflow.com/問題/ 243794/jquery-ui-tabs-causing-screen-to-jump) – 2015-01-23 21:46:42
不完全一樣但非常相似。 – HNygard 2015-01-24 21:06:51