jQuery-通过网址选择标签 [英] Jquery - Select tab via url

查看:116
本文介绍了jQuery-通过网址选择标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jquery代码,可让我使用制表符,制表符和内容/div之间的第四个.我的问题是使用下面的代码,如何根据url中的变量加载初始标签?因此,如果我转到somesite.com/page.php#tab2,则默认情况下,tab2加载不是tab1加载:

I have the following jquery code that allows me to use tabs, to tab back and fourth between content/divs. My question is using the code below, how could I have it load the initial tab based on a variable in the url? So if I go to somesite.com/page.php#tab2, then instead of tab1 loading by default tab2 loads:

$('.tabcontent > div').hide();
$('.tabcontent > div:first-child').show();
$('.tabs > li:first-child').addClass('selected');
$('.tabs > li a').click(function() {
    var tab_id = $(this).attr('href');
    $(tab_id).parent().children().hide();
    $(tab_id).fadeIn();
    $(this).parent().parent().children().removeClass('selected');
    $(this).parent().addClass('selected');
    return false;
});

编辑

仅需注意此代码是一个click事件,但是,如果我想通过人员加载的网址更改标签,那么除非他们单击标签,否则它将无法正常工作.那么,如何获得它来监听点击并监听当前网址中是否存在值?

Just to note this code is a click event, however if I want to change the tab via the url the person loads then it wont work unless they click a tab. So how can I get it to listen for a click and listen to see if there is a value present in the current url?

推荐答案

您可以使用gup函数,在此处找到:

You can use gup function,found here:

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

这将使您可以读取URL参数,并使用它浏览到选定的标签.

This will allow you to read a URL parameter and use it to navigate to the selected tab.

这篇关于jQuery-通过网址选择标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆