我的jquery标签代码有什么问题?

what is wrong with my jquery tabber code?

本文关键字:什么 问题 代码 jquery 标签 我的      更新时间:2023-09-26

救命啊!我很沮丧,不知道我做错了什么。我的代码有什么问题?它根据URL激活一个特定的选项卡。例如,www.mywebsite.com#tab3将激活tab3中的tab3。这就是我如何实现这一点:我使用location.hash来获取位置并与href进行比较,然后激活该选项卡。但这里存在的问题:我有两个不同风格的标签(ul#tabs li aul.tabs li a)。我的选择和比较做得好吗?这是代码:

 var hash = location.hash;

 $(".tab_content").hide(); //Hide all content
 if ($("ul#tabs li a[href='" + hash + "'], ul.tabs li a[href='" + hash + "']").length) {
     $("ul#tabs li a[href='" + hash + "'], ul.tabs li a[href='" + hash + "']").parent().addClass("active"); //Activate tab
     $(hash).show();
 }

您可以使用一个函数来实现此操作:

var hash = location.hash;
function active_hash(type) {
    var $node = $("ul" + type + "tabs li a[href='" + hash + "'], ul.tabs li a[href='" + hash + "']");
    if ($node.length) {
        $node.parent().addClass("active");
        $(hash).show();
    }
}
active_hash('#');
active_hash('.');