ページをロードすると、アドレスバーにアンカーされたタブIDの代わりに各タブが表示されます。呼び出されたアンカーID以外のすべてがロードまたはリフレッシュ時に表示されないようにするにはどうすればよいですか? JavaScriptを引き継ぐ前に、これは.content
要素のいずれかが表示さからブラウザを防ぐことができますハッシュアンカータブをページロード時にのみ表示
<style>
.content { display: none; }
</style>
:
<script>
//Grab hash off URL (default to first tab) and update
$(window).bind("hashchange", function(e) {
var anchor = $(location.hash);
if (anchor.length === 0) {
anchor = $(".tabs div:eq(0)");
}
updateTabs(anchor);
});
//Pass in the tab and show appropriate contents
function updateTabs(tab) {
$(".tabs #tab a")
.removeClass("active")
.filter(function(index) {
return $(this).attr("href") === '#' + tab.attr("id");
}).addClass("active");
$(".tabs .content").hide();
tab.show();
}
//Fire the hashchange event when the page first loads
$(window).trigger('hashchange');
</script>
<div class="tabs">
<ul>
<li class="tab"><a href="#div1">Tab 1</a></li>
<li class="tab"><a href="#div2">Tab 2</a></li>
<li class="tab"><a href="#div3">Tab 3</a></li>
</ul>
<div id="div1" class="content">Div 1</div>
<div id="div2" class="content">Div 2</div>
<div id="div3" class="content">Div 3</div>
</div>
それは私のために正常に動作します。 [this jsFiddle](http://jsfiddle.net/gothick/YRjpU/1/)を参照してください。あなたが問題を抱えている特定のブラウザはありますか?あなたは間違いなくjQueryを含んでいますか?あなたのエラーJavascriptコンソール? –
私はFirefoxを使い、$(document).ready(function(){jscode})を追加しました。それはトリックでした。 – Kim