jquery选项卡,基于URL可见

jquery选项卡,基于URL可见,jquery,tabs,click,Jquery,Tabs,Click,我使用jquery和基于 正在查找锚定值,而不是URL 希望这是有意义的 我(认为)如果有可能修复,我需要一种方法从URL获取#选项卡,并基于单击的锚 谢谢您可以使用window.location.hash检索URL的#something部分。见: 还有,你发布的代码。。。可能是一个很好的清单,列出了在jQuery中不应该做的事情。让我们为您修复它: $(function() { var tabContent = $(".tab_content"); // Modified t

我使用jquery和基于

正在查找锚定值,而不是URL

希望这是有意义的

我(认为)如果有可能修复,我需要一种方法从URL获取#选项卡,并基于单击的锚


谢谢

您可以使用
window.location.hash
检索URL的
#something
部分。见:


还有,你发布的代码。。。可能是一个很好的清单,列出了在jQuery中不应该做的事情。让我们为您修复它:

$(function() {
    var tabContent = $(".tab_content");
    // Modified tutorial's code for this
    var tabs = $("#menu li");
    var hash = window.location.hash;

    tabContent.not(hash).hide();
    tabs.find('[href=' + hash + ']').addClass('active');

    tabs.click(function() {
        $(this).addClass('active').siblings().removeClass('active');
        tabContent.hide();
        var activeTab = $(this).find("a").attr("href");

        $(activeTab).fadeIn();
        return false;
    });
});
是,请尝试:

$('a[href="'+activeTab'"]').fadeIn();

是否要在加载页面时显示选项卡

 $(function() {
      $("ul#menu li").removeClass("active"); //Remove any "active" class  
      $(".tab_content").hide(); //Hide all tab content  

      // set the active class on the tab where the href ends with #tabN
      $("ul#menu li a[href$='" + window.location.hash + "]").closest("li").addClass("active");
      // use the #tabN part of the url as the id selector to show the content
      $(window.location.hash).fadeIn();
 });
此外,在onclick处理程序中,可能需要替换该行

    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 


要获取href.

的#tabN部分,那么tab4指的是什么?活动选项卡的锚的id?例如:我的选项卡位于“tabs.html”上。我有“page.html”和“page.html”上的链接,链接到“tabs.html#tab4”。我希望能够单击tabs.html#tab4并转到tabs.html上的第四个选项卡。目前它只显示“第一个”选项卡-忽略“请求的”选项卡。希望这能澄清,谢谢。谢谢。将比较这两组代码,并尝试找出您的版本的不同之处/更好之处。非常感谢。@Ross-基本上,尽可能地缓存jQuery对象(每次执行
$()
时,它会创建一个新的jQuery对象,这非常昂贵,因此将其存储在变量中),并尽可能地进行链接。这很有意义,即使我自己不知道如何正确实现它,谢谢!
 $(function() {
      $("ul#menu li").removeClass("active"); //Remove any "active" class  
      $(".tab_content").hide(); //Hide all tab content  

      // set the active class on the tab where the href ends with #tabN
      $("ul#menu li a[href$='" + window.location.hash + "]").closest("li").addClass("active");
      // use the #tabN part of the url as the id selector to show the content
      $(window.location.hash).fadeIn();
 });
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 
    var activeTab = $(this).find("a")[0].hash; //Find the rel attribute value to identify the active tab + content