有点逻辑 上代码

thml布局

点击查看代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>面向对象 Tab</title>
    <link rel="stylesheet" href="./styles/tab.css">
    <link rel="stylesheet" href="./styles/style.css">
</head>

<body>

    <main>
        <h4>
            Js 面向对象 动态添加标签页
        </h4>
        <div class="tabsbox" id="tab">
            <!-- tab 标签 -->
            <nav class="fisrstnav">
                <ul>
                    <li class="liactive"><span>测试1</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试2</span><span class="iconfont icon-guanbi"></span></li>
                    <li><span>测试3</span><span class="iconfont icon-guanbi"></span></li>
                </ul>
                <div class="tabadd">
                    <span>+</span>
                </div>
            </nav>

            <!-- tab 内容 -->
            <div class="tabscon">
                <section class="conactive">测试1</section>
                <section>测试2</section>
                <section>测试3</section>
            </div>
        </div>
    </main>

    <script src="./tabs.js"></script>
</body>

</html>

css样式

点击查看代码
* {
    margin: 0;
    padding: 0;
}

ul li {
    list-style: none;
}

main {
    width: 960px;
    height: 500px;
    border-radius: 10px;
    margin: 50px auto;
}

main h4 {
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.tabsbox {
    width: 900px;
    height: 400px;
    margin: 0 auto;
    border: 1px solid lightsalmon;
    position: relative;
}

nav ul {
    overflow: hidden;
}

nav ul li {
    float: left;
    width: 100px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-right: 1px solid #ccc;
    position: relative;
}

nav ul li.liactive {
    border-bottom: 2px solid #fff;
    z-index: 9;
}

#tab input {
    width: 80%;
    height: 60%;
}

nav ul li span:last-child {
    position: absolute;
    user-select: none;
    font-size: 12px;
    top: -18px;
    right: 0;
    display: inline-block;
    height: 20px;
}

.tabadd {
    position: absolute;
    /* width: 100px; */
    top: 0;
    right: 0;
}

.tabadd span {
    display: block;
    width: 20px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    border: 1px solid #ccc;
    float: right;
    margin: 10px;
    user-select: none;
}

.tabscon {
    width: 100%;
    height: 300px;
    position: absolute;
    padding: 30px;
    top: 50px;
    left: 0px;
    box-sizing: border-box;
    border-top: 1px solid #ccc;
}

.tabscon section,
.tabscon section.conactive {
    display: none;
    width: 100%;
    height: 100%;
}

.tabscon section.conactive {
    display: block;
}

删除iconfont字体图标

点击查看代码
@font-face {font-family: "iconfont";
  src: url('./iconfont/iconfont.eot?t=1553960438096'); /* IE9 */
  src: url('./iconfont/iconfont.eot?t=1553960438096#iefix') format('embedded-opentype'), /* IE6-IE8 */
  url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAK4AAsAAAAABmwAAAJrAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCCcAp4fwE2AiQDCAsGAAQgBYRtBzAbpQXIrrApw71oi3CCOyzEy8RvE4yIN8TD036/zp03qCYRjaJZNBFFS/gREoRGipQKofjuNrb+9XbTqrmXcqWzfTRDqFqWkhAJzYToaE6LQ7Q30CirRqSKMnj58DdIdrNAdhoTQJa5VGfLrtiAy+lPoAcZdUC57UljTR4TMAo4oL0xiqwYG8YueIHPCdTqYajty/t+bUpmrwvEnUK42lQhLMssVy1UNhzN4kmF6vSQVvMY/T5+HEU1SUXBbti7uBBrx++cgqJULp0GhAgBna5AgSkgE0eN6R1NwTitNt0yAI5VG7wr/8AljmoX7K+zq+tBF1Q8k9JTPWp1AjnJDgCzmM3bU0V31dsvV3M2eC6fHjaGfX/qS7U5Gr58vj6uD0bgxudyrV/OtHHyP+NZnpO1txbktjdY+3FB61+7nxeOzq8niGYnRwT3v3aZxeXf6rrNxl5//49WlEtZUUL1Pj3Bv1EO7MuG2namrCkbvcnApLUJtWpRhv2tzlRLx43kQ7WO2/FW6c5QqDZEZnYKFeosoVK1NdSa5E/XaVM1Ra7BhAEQmk0kjV5QaLbIzG5U6HRRqTkK1DqJtivrjMT1zJaNnIsihAiyQE3JdbszcW0Xiadzdl4d8UO0HSUGNDNXzl2hifYSO5pPjrorgdjUAAavoa5TKDZVUXD3kuuOOzh70fShvUiN2owtNsRxIREIIiATUCYpGO2aqXy/CxEeHcfuaKrLDiGbQ5kcEMsNIK8M5qCmR3mn8RFHOpcECBtlAAwWIZ2OAqV5kQoJXHvShORYBzrDZKhhb3uT8QPlrA3bmsKZV6i89DiTV2o1AAAA') format('woff2'),
  url('./iconfont/iconfont.woff?t=1553960438096') format('woff'),
  url('./iconfont/iconfont.ttf?t=1553960438096') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  url('./iconfont/iconfont.svg?t=1553960438096#iconfont') format('svg'); /* iOS 4.1- */
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-guanbi:before {
  content: "\e676";
}

js代码

点击查看代码
var that;
class Tab {
    constructor(id) {
        // 定义全局this
        that = this;
        // 获取元素
        this.main = document.querySelector(id)
        //获取加号按钮
        this.add = this.main.querySelector('.tabadd')

        //获取li的父元素
        this.ul = this.main.querySelector('.fisrstnav ul:first-child')
        //获取section的父元素
        this.fsection = this.main.querySelector('.tabscon')
        this.init();
    }
    //初始化的方法 处理所有点击事件
    init() {
        this.updateNode();
        this.add.onclick = this.addTab;
        //init初始化操作让相关的元素绑定事件
        for (var i = 0; i < this.lis.length; i++) {
            // 给li添加一个索引
            this.lis[i].index = i;
            //因为点击做切换功能 所以点击了li直接等于toggleTab切换操作函数
            this.lis[i].onclick = this.toggleTab;
            //x号按钮跟小li是一样的 所以写在li的for循环里
            this.remove[i].onclick = this.removeTab;
            //给每一个span添加双击事件
            this.spans[i].ondblclick = this.editTab;
            //给每一个sections添加双击事件
            this.sections[i].ondblclick = this.editTab;

        }
    }

    //获取所有小li和section
    updateNode() {
        //获取所有的小li
        this.lis = this.main.querySelectorAll('li')
        //获取所有的section
        this.sections = this.main.querySelectorAll('section')
        //获取x号关闭的按钮
        this.remove = this.main.querySelectorAll('.icon-guanbi')
        //获取所有的span
        this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child')
    }

    //1、切换功能
    toggleTab() {
        that.clearClass();
        //点击了li就给他的class添加liactive类名
        this.className = 'liactive'
        //点击li内容也跟着变所以
        that.sections[this.index].className = 'conactive'
    }
    //点击每个li就要清楚其他样式 所以定义一个专门清楚样式函数
    clearClass() {
        //再次循环li
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].className = '';
            this.sections[i].className = '';
        }
    }
    //2、添加功能
    addTab() {
        //添加之前清除掉所以小li
        that.clearClass()
        //生成一个随机数
        var random = Math.random()
        //1.点击创建li元素和section元素
        var li = ' <li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"></span></li>'

        var section = '<section class="conactive">测试' + random + '</section>'

        //2.把这两个元素追加到对应的父元素里面
        //以前创建元素createElement,但是元素里面内容比较多,需要innerHTML复制在appendChild追加到父元素里面 这样是比较繁琐的
        //现在有个高级做法:利用insertAdjacentHTML()可以直接把字符串元素添加到父元素中 afterbegin在元素内容的第一个子节点之前插入beforeend插入元素内容最后一个子节点
        that.ul.insertAdjacentHTML('beforeend', li)
        that.fsection.insertAdjacentHTML('beforeend', section)
        that.init();
    }
    //3、删除功能
    removeTab(e) {
        //阻止冒泡 防止触发li的切换点击事件
        e.stopPropagation();
        //他的爸爸li有索引 所以index=他爸爸的索引parentNode.index
        var index = this.parentNode.index
        console.log(index);
        //根据索引删除对应的li和section
        that.lis[index].remove();
        that.sections[index].remove();
        //删除完要重新获取所有的元素
        that.init();
        //当我们删除的不是选中状态li的时候,原来的选中状态li保持不变 只有一个if不需要花括号
        if (document.querySelector('.liactive')) return
        //当我们删除了选中状态的这个li的时候,让它当前一个li处于选定状态
        index--;
        //手动调用我们的点击事件 不需要鼠标触发
        that.lis[index] && that.lis[index].click()
    }
    //4、修改功能
    editTab() {
        // 此函数方法的this指向span
        //一双就获取原先文本
        var str = this.innerHTML;
        //双击代码禁止选定文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.getSelection.empty()
        console.log('双击了');
        this.innerHTML = '<input type="text" />';
        //input是他的第一个孩子
        var input = this.children[0];
        input.value = str
        //让文本框里面的文字处于选定状态
        input.select();
        //当鼠标离开文本框(失去焦点)把文本框里面的值给span
        input.onblur = function () {
            //input的父亲的值parentNode.innerHTML===等于自己的值
            this.parentNode.innerHTML = this.value
        }
        //当按下回车键的时候可以把文本框里面的值给span
        input.onkeyup = function (e) {
            if (e.keyCode === 13) {
                //手动调用表单失去焦点事件 不需要鼠标离开操作
                this.blur()
            }
        }
    }

}

// 实例化tab对象 传给constructor接收 
new Tab('#tab');

// 创建一个实例
// var tab = new Tab('#tab');
// 这样比较繁琐
// tab.init();