开发者

Loading tab by default in jQuery tab script

开发者 https://www.devze.com 2023-03-18 08:00 出处:网络
I have a jQuery tab script that gets content from a PHP file defined by the link and parses 开发者_StackOverflow社区it to a div element. When a user selects a tab, it turns bold. However, currently wh

I have a jQuery tab script that gets content from a PHP file defined by the link and parses 开发者_StackOverflow社区it to a div element. When a user selects a tab, it turns bold. However, currently when the script is loaded up, a tab is not selected by default. My question is how can I load up a certain tab and it's contents by default and show that the tab is selected with my current code?

This is my current jQuery code:

function load(url){
    $.ajax({
        url:url,
        success:function(message){
            $("#content").html(message);
        }
    });
}

$(document).ready(function(){
    $("[id]").click(function(){
        type=$(this).attr("id");
        url=""+type+".php";
        $("[id]").removeClass("selected");
        $("#"+type).addClass("selected");
        load(url);
        return false;
    });
});

This is my HTML code:

<ul> 
<li><a id="tab1" href="javascript:void(null);">Tab1</a></li> 
<li><a id="tab2" href="javascript:void(null);">Tab2</a></li> 
<li><a id="tab3" href="javascript:void(null);">Tab3</a></li> 
</ul>


Trigger the click event of the tab you want to load: e.g. -

$(document).ready(function(){
    $("[id]").click(function(){
        type=$(this).attr("id");
        url=""+type+".php";
        $("[id]").removeClass("selected");
        $("#"+type).addClass("selected");
        load(url);
        return false;
    });
    //load default tab
    $("#tab1").click();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消