tab页签添加关闭

目前市场上的ui框架貌似都没有页签添加关闭功能,学习网上的代码,整合了一个实例

页签添加、关闭实例:

<!DOCTYPE html>
<html lang="ch-cn">
<head>
      <meta charset="utf-8">
      <script src="../skeleton/jquery-1.11.3.js"></script>
    <style type="text/css">
        html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
            margin: 0;
            padding:0;
        }
        *{
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }
        ul, ol {
            margin-top: 0;
            margin-bottom: 10px;
        }
        a {
            color: #428bca;
            text-decoration: none;
        }
        .fa {
            display: inline-block;
            font-family: FontAwesome;
            font-style: normal;
            font-weight: normal;
            line-height: 1;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
        .aui_close {
            display: inline-block;
            font-family: FontAwesome;
            font-style: normal;
            font-weight: normal;
            line-height: 1;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
            cursor: pointer;
        }
        .aui_close:hover {
            color: #FFCC33 !important;
        }
        .tabbable {
            border: none;
            margin-bottom: 10px;
        }
        .nav {
            padding-left: 0;
            margin-bottom: 0;
            list-style: none;
            text-align: center;
        }
        .nav>li {
            position: relative;
            display: block;
        }
        .nav-tabs {
            margin-bottom: 0;
            margin-left: 0;
            border: 0;
            top: 2px;
            background-color: #f3f3f3;
            -webkit-box-shadow: 0 0 4px rgba(0,0,0,.3);
            -moz-box-shadow: 0 0 4px rgba(0,0,0,.3);
            box-shadow: 0 0 4px rgba(0,0,0,.3);
            border-bottom: 1px solid #ddd;
        }
        .nav-tabs>li {
            float: left;
            margin-bottom: 0px;
            margin-left: 5px;
        }
        .nav-tabs li i {
            position: relative;
            margin-left: 10px;
            cursor: pointer;
        }
        .nav-tabs li [class*=" fa-"], .nav-tabs li [class^="fa-"] {
            width: 1.25em;
            display: inline-block;
            text-align: center;
        }
        #myTab {
            height: 35px;
            text-align: center;
            overflow: hidden;
        }
        #myTab > li {
            height: 35px;
            overflow: hidden;
            margin-top: 5px;
            background: #dedede;
            background: rgba(0,0,0,0.05);
            color: #777;
        }
        #myTab > li.active, #myTab>li:hover, #myTab>li:focus {
            background-color: #fff;
            border-bottom-color: transparent;
            box-shadow: 0 -2px 3px 0 rgba(0,0,0,.15);
        }
        #myTab > li > a {
            position: relative;
            display: inline-block;
            white-space: nowrap;
            overflow: hidden;
            padding: 8px 0px 8px 10px;
            margin: 0 20px 0 0;
            border-top: 0px;
            box-shadow: none;
            background: transparent;
            line-height: 17px;
            border: 0;
            max-width: 108px;
            color: #777;
        }
        #myTab > li.active {
            border-top: 2px solid #2dc3e8;
        }
        #myTab li [class*=" fa-"], #myTab li [class^="fa-"] {
            vertical-align: middle;
            margin-left: 0px;
            position: absolute;
            right: 2px;
            margin-top: 10px;
        }
        .tab-content {
            background: none;
            padding: 0;
            padding-top: 5px;
            position: relative;
        }
        .tab-content > div{
            display: none;
        }
        .tab-content > div.active{
            display: block;
        }
    </style>
</head>
<body>

<div class="tabbable" id="tabs" style="border:none;">
    <!-- 页面标签列表 -->
    <ul class="nav nav-tabs" id="myTab">
    </ul>
    <!-- 页面内容列表,和页面标签列表对应 -->
    <div class="tab-content">
    </div>
</div>
</body>
<script type="text/javascript">

    //切换tab页的显示
    $(document).on('click','#myTab > li',function(e){
        //清除原来显示的tab页
        var oldTab = $("#myTab li.active").removeClass("active").find("a[data-toggle='tab']");
        $(oldTab.attr("href")).removeClass("active");

        //设置新的显示tab页
        var newTab = $(this).addClass("active").find("a[data-toggle='tab']");
        $(newTab.attr("href")).addClass("active");

        refreshTabHistory(false/*isDelete*/,$(this).attr('id').substring(4));
    })
    //手动调用切换到要显示的tab页,当前的action只支持show
    //eg:$("#tab-0 a[data-toggle='tab']").tab("show");
    $.fn.tab = function(action){
        if(action == "show"){
            $(this).parent().click();
        }
    }

    var currentTabId = '';//当前焦点Tab
    //在非左侧菜单栏弹出的tab页也会用到该数据,如common.js中的pageForward函数
    var pageCounter = 0;
    /*
    id:       tab页签的html标签ID属性格式为"tab-"+id,内容容器的html标签ID格式为"tab-content-"+id
    text:     tab页签的显示文本
    url:      打开的iframe的url
    innerTab: 是否是内部弹出页(打开的tab页触发添加新的tab页),默认为undefined/false
    */
    function addTab(id,text,url,innerTab) {
        //如果某个页面已经打开,则切换到该页显示即可,不会新添加tab页
        if($('#myTab #tab-'+id).length > 0){
            $('#myTab  #tab-' + id + ' a').tab('show');
        }else{
            var tab_id = "tab-" + id,
                tab_content_id = "tab-content-"+id;

            //添加tab页签
            $("#myTab > li").removeClass("active");
            $("#myTab").append("<li id='" + tab_id + "' class='active'><a data-toggle='tab' href='#"
                + tab_content_id + "'>" + text + "</a>"
                + ("<a class='aui_close'onMouseOver='this.style.color=#FFCC33' onclick='deleteTab(\"" + id + "\")'>×</a>") + "</li>");

            //添加新的内容显示
            $(".tab-content > div").removeClass("active");
            $(".tab-content").append("<div id='"+ tab_content_id +"' class='active'>"
                + "<iframe id='iframepage" + (pageCounter++) + "' name='iframepage" + (pageCounter++)
                + "' frameborder='0' marginwidth='0' marginheight='0' valign='middle' width='100%' height='550' scrolling='no' resize='no'  src='" + url + "'></iframe></div>");
        }
        //刷新切换tab的历史记录
        refreshTabHistory(false/*isDelete*/,id);
    }
    //参数id为tab的标志,但是并不是tab页的id属性,真正的id属性值是"tab-"+id
    function deleteTab(id){
        var tabJQ = $("#tab-"+id),
            tabContentJQ = $("#tab-content-" + id);

        if(!tabJQ.hasClass("active")){
            tabJQ.remove();
            tabContentJQ.remove();
            refreshTabHistory(true/*isDelete*/,id);
        }else{
            tabJQ.remove();
            tabContentJQ.remove();
            refreshTabHistory(true/*isDelete*/,id);
            $('#tab-' + currentTabId + ' > a').tab('show').click();
        }
    }
    //关闭当前tab页的快速方法
    function closeCurrentTab(){
        deleteTab(currentTabId);
    }

    /*
    刷新页签切换历史
    isdelete: 是否是删除tab页签,true:是,false:否
    curTabId:要处理的tab页签的id,tab页签html标签元素的ID属性格式为"tab-"+curTabId
    */
    function refreshTabHistory(isdelete,curTabId){
        if(!refreshTabHistory.histoty){
            //用来记录用户点击tab的历史
            refreshTabHistory.histoty = [];
        }
        var index = 0,
            leng = refreshTabHistory.histoty.length;
        //查找传入的tab页签在历史记录中的位置
        for(; index < leng; index++){
            if(refreshTabHistory.histoty[index] == curTabId){
                break;
            }
        }

        //如果是删除页签,直接在历史记录中删除即可,历史记录的其他页签的顺序不变
        if(isdelete){
            refreshTabHistory.histoty.splice(index,1);

            //如果是新增页签,先保证历史记录中没有改页签(有就删掉),然后将新增的页签放在历史记录的最后面(即该页签为最新)
        }else{
            if(index < leng) {
                refreshTabHistory.histoty.splice(index,1);
            }
            refreshTabHistory.histoty.push(curTabId);
        }
        currentTabId = refreshTabHistory.histoty[refreshTabHistory.histoty.length - 1];
    }
    //测试代码
    for(var i = 0; i < 3; i++){
        addTab(i,"test" + i,"../test.html");
    }
</script>
</html>

效果图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值