jQuery - check URL for anchor
I made a really simple tab interface using jQuery based on jQuery minitabs:
$(".tabs .nav").delegate("a", "click"开发者_开发问答, function(){
var tabs = $(this).closest(".tabs");
var target = $(this).attr("href").split('#')[1];
$(".nav a", tabs).removeClass("active");
$(this).addClass("active");
$("input.active-tab", tabs).val(target);
//hide the tabs that doesn't match the ID
$('.tab:not(.'+target+')', tabs).hide();
$('.tab.'+target, tabs).show();
return false;
});
the markup looks like:
<div class="tabs">
<input type="hidden" class="active-tab" name="active-tab" value="tab1" />
<ul class="nav">
<li><a href="#tab1" class="active">tab1</a></li>
<li><a href="#tab2">tab2</a></li>
<li><a href="#tab3">tab3</a></li>
</ul>
<div class="tab tab1">
....
</div>
<div class="tab tab2">
....
</div>
<div class="tab tab3">
....
</div>
</div>
It works fine, but I want to make so the active tab can be set based on the current URL.
for eg. if I paste this URL: http://site.com/page.php#tab2
, I want the tab2 to appear as active
How can I do that?
that is located in window.location.hash
you can search $(".tabs .nav")
for a link that has the same hash and trigger a click on it
精彩评论