jquery動態增減選項卡

Love丶蘭發表於2016-10-12

jquery動態增減選項卡

最近需要用到tab標籤欄,百度了一個,感覺挺不錯的,就拿來用了一下,修改了一點點屬性,感覺很適合,就拿給大家分享,在有網的情況下,程式碼複製過去就能使用,沒網,就只有自己找一個js的檔案也行!

效果圖:


下面程式碼附上(此程式碼是被我刪減後的程式碼):
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery動態增減選項卡</title>
 <style type="text/css">
        body { font-family:Lucida Sans, Lucida Sans Unicode, Arial, Sans-Serif; font-size:13px; margin:0px auto;}
        #tabs { margin:0; padding:0; list-style:none; overflow:hidden; }
        #tabs li { float:left; display:block; padding:5px; background-color:#bbb; margin-right:5px;}
        #tabs li a { color:#fff; text-decoration:none; }
        #tabs li.current { background-color:#e1e1e1;}
        #tabs li.current a { color:#000; text-decoration:none; }
        #tabs li a.remove { color:#f00; margin-left:10px;}
        #content { background-color:#e1e1e1;}
        #content p { margin: 0; padding:20px 20px 100px 20px;}
        
        #main { width:900px; margin:0px auto; overflow:hidden;background-color:#F6F6F6; margin-top:20px;
             -moz-border-radius:10px;  -webkit-border-radius:10px; padding:30px;}
        #wrapper, #doclist { float:left; margin:0 20px 0 0;}
        #doclist { width:150px; border-right:solid 1px #dcdcdc;}
        #doclist ul { margin:0; list-style:none;}
        #doclist li { margin:10px 0; padding:0;}
        #documents { margin:0; padding:0;}
        
        #wrapper { width:700px; margin-top:20px;}
        
    </style>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <!-- <script type="text/javascript" src="jquery-1.4.min.js" ></script> -->
    <script type="text/javascript">
        $(document).ready(function() {
            $("#documents a").click(function() {
                addTab($(this));
            });

            $('#tabs a.tab').live('click', function() {
                // Get the tab name 獲取tab名稱
                var contentname = $(this).attr("id") + "_content";

                // hide all other tabs 隱藏其他tab
                $("#content p").hide();
                $("#tabs li").removeClass("current");

                // show current tab 顯示當前tab
                $("#" + contentname).show();
                $(this).parent().addClass("current");
            });

            $('#tabs a.remove').live('click', function() {
                // Get the tab name 獲取tab名稱
                var tabid = $(this).parent().find(".tab").attr("id");

                // remove tab and related content 刪除標籤和相關內容 
                var contentname = tabid + "_content";
                $("#" + contentname).remove();
                $(this).parent().remove();

                // if there is no current tab and if there are still tabs left, show the first one 如果沒有當前的標籤,如果仍然有標籤左,顯示第一個
                if ($("#tabs li.current").length == 0 && $("#tabs li").length > 0) {

                    // find the first tab     找到第一個標籤
                    var firsttab = $("#tabs li:first-child");
                    firsttab.addClass("current");

                    // get its link name and show related content 獲取它的連結名稱並顯示相關內容
                    var firsttabid = $(firsttab).find("a.tab").attr("id");
                    $("#" + firsttabid + "_content").show();
                }
            });
        });
        function addTab(link) {
            // hide other tabs 隱藏其他的標籤
            $("#tabs li").removeClass("current");
            $("#content p").hide();
            
            // If tab already exist in the list, return  如果選項卡中已經存在於列表中,返回
            if ($("#" + $(link).attr("rel")).length != 0){
				//當前標籤  高亮
				$("#" + $(link).attr("rel")).parent().addClass("current");
				//顯示當前標籤的內容
				$("#" + $(link).attr("rel") + "_content").show();
                return;
            }
			
            // add new tab and related content 新增新標籤和相關內容 
            $("#tabs").append("<li class='current'><a class='tab' id='" +
                $(link).attr("rel") + "' href='#'>" + $(link).html() + 
                "</a><a href='#' class='remove'>x</a></li>");

            $("#content").append("<p id='" + $(link).attr("rel") + "_content'>" + 
                $(link).attr("title") + "</p>");
            
            // set the newly added tab as current 將新新增的標籤設定為當前
            $("#" + $(link).attr("rel") + "_content").show();
        }
    </script>
</head>
<body>
    <div id="main">
    <div id="doclist">

        <ul id="documents">
            <li><a href="#" rel="Document1" title="This is the content of Document1" id="1110" >Document1</a></li>
            <li><a href="#" rel="Document2" title="This is the content of Document2" >Document2</a></li>
            <li><a href="#" rel="Document3" title="This is the content of Document3" >Document3</a></li>
            <li><a href="#" rel="Document4" title="This is the content of Document4" >Document4</a></li>
            <li><a href="#" rel="Document5" title="This is the content of Document5" >Document5</a></li>

        </ul>
    </div>
    <div id="wrapper">
        <ul id="tabs">
            <!-- Tabs go here -->
        </ul>
        <div id="content">
            <!-- Tab content goes here -->
        </div>

    </div>
    </div>
</body>
</html>


原始碼附上:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery動態增減選項卡 - 站長素材</title>
 <style type="text/css">
        body { font-family:Lucida Sans, Lucida Sans Unicode, Arial, Sans-Serif; font-size:13px; margin:0px auto;}
        #tabs { margin:0; padding:0; list-style:none; overflow:hidden; }
        #tabs li { float:left; display:block; padding:5px; background-color:#bbb; margin-right:5px;}
        #tabs li a { color:#fff; text-decoration:none; }
        #tabs li.current { background-color:#e1e1e1;}
        #tabs li.current a { color:#000; text-decoration:none; }
        #tabs li a.remove { color:#f00; margin-left:10px;}
        #content { background-color:#e1e1e1;}
        #content p { margin: 0; padding:20px 20px 100px 20px;}
        
        #main { width:900px; margin:0px auto; overflow:hidden;background-color:#F6F6F6; margin-top:20px;
             -moz-border-radius:10px;  -webkit-border-radius:10px; padding:30px;}
        #wrapper, #doclist { float:left; margin:0 20px 0 0;}
        #doclist { width:150px; border-right:solid 1px #dcdcdc;}
        #doclist ul { margin:0; list-style:none;}
        #doclist li { margin:10px 0; padding:0;}
        #documents { margin:0; padding:0;}
        
        #wrapper { width:700px; margin-top:20px;}
            
        #header{ background-color:#F6F6F6; width:900px; margin:0px auto; margin-top:20px;
             -moz-border-radius:10px;  -webkit-border-radius:10px; padding:30px; position:relative;}
        #header h2 {font-size:16px; font-weight:normal; margin:0px; padding:0px;}

    </style>
    
    <script type="text/javascript" src="jquery-1.4.min.js" ></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#documents a").click(function() {
                addTab($(this));
            });

            $('#tabs a.tab').live('click', function() {
                // Get the tab name
                var contentname = $(this).attr("id") + "_content";

                // hide all other tabs
                $("#content p").hide();
                $("#tabs li").removeClass("current");

                // show current tab
                $("#" + contentname).show();
                $(this).parent().addClass("current");
            });

            $('#tabs a.remove').live('click', function() {
                // Get the tab name
                var tabid = $(this).parent().find(".tab").attr("id");

                // remove tab and related content
                var contentname = tabid + "_content";
                $("#" + contentname).remove();
                $(this).parent().remove();

                // if there is no current tab and if there are still tabs left, show the first one
                if ($("#tabs li.current").length == 0 && $("#tabs li").length > 0) {

                    // find the first tab    
                    var firsttab = $("#tabs li:first-child");
                    firsttab.addClass("current");

                    // get its link name and show related content
                    var firsttabid = $(firsttab).find("a.tab").attr("id");
                    $("#" + firsttabid + "_content").show();
                }
            });
        });
        function addTab(link) {
            // If tab already exist in the list, return
            if ($("#" + $(link).attr("rel")).length != 0)
                return;
            
            // hide other tabs
            $("#tabs li").removeClass("current");
            $("#content p").hide();
            
            // add new tab and related content
            $("#tabs").append("<li class='current'><a class='tab' id='" +
                $(link).attr("rel") + "' href='#'>" + $(link).html() + 
                "</a><a href='#' class='remove'>x</a></li>");

            $("#content").append("<p id='" + $(link).attr("rel") + "_content'>" + 
                $(link).attr("title") + "</p>");
            
            // set the newly added tab as current
            $("#" + $(link).attr("rel") + "_content").show();
        }
    </script>
</head>
<body>
    <div id="header">
    <h2>jquery動態增減選項卡是一款可以通過按鈕自己新增和刪除選項卡,支援選項卡數量、內容定製。</h2>
        <img src="help.png" alt="Click on lnks to open 'documents' in tabs" style="position:absolute;left:-170px; top:200px;" />
    </div>
    <div id="main">
    <div id="doclist">
        <h2>Documents</h2>

        <ul id="documents">
            <li><a href="#" rel="Document1" title="This is the content of Document1">Document1</a></li>
            <li><a href="#" rel="Document2" title="This is the content of Document2">Document2</a></li>
            <li><a href="#" rel="Document3" title="This is the content of Document3">Document3</a></li>
            <li><a href="#" rel="Document4" title="This is the content of Document4">Document4</a></li>
            <li><a href="#" rel="Document5" title="This is the content of Document5">Document5</a></li>

        </ul>
    </div>
    <div id="wrapper">
        <ul id="tabs">
            <!-- Tabs go here -->
        </ul>
        <div id="content">
            <!-- Tab content goes here -->
        </div>

    </div>
    </div>
<div style="text-align:center;clear:both">
<p>適用瀏覽器:IE8、360、FireFox、Chrome、Opera、傲遊、搜狗、世界之窗. 不支援Safari。</p>
<p>來源:<a href="http://sc.chinaz.com/" target="_blank">站長素材</a></p>
</div>
</body>
</html>




相關文章