欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Js 面向对象动态添加标签页

程序员文章站 2024-03-23 09:57:46
...

效果图
Js 面向对象动态添加标签页
直接上代码

<!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/style.css">
    <style>
		* {
    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;
    margin: 0 auto;
    height: 400px;
    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;
	}
    </style>
</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="js/tab.js"></script>
</body>
</html>
//抽象 class 类 Tad
// var that;
class Tab {
    constructor(id) {
        // that = this;
        //获取元素 this 实例化对象
        this.main = document.querySelector(id); //最大的id盒子
        //获取加号
        this.tabadd = this.main.querySelector('.tabadd');
        //获取ul
        this.ul = this.main.querySelector('.fisrstnav ul');
        //获取section 父亲
        this.tabscon = this.main.querySelector('.tabscon');
        this.init();
    }
    updateNode() {
        this.lis = this.main.querySelectorAll('li') //所有的li
        this.sections = this.main.querySelectorAll('section'); //所有的section
        //获取li所有的 第一个 span
        this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child')
    }
    //把事件专门放到方法里面 初始化 所有的事件
    init() { //因为li有多个所以需要遍历所有的li
        this.updateNode();
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i; //遍历得出当前li里的值的索引
            //li点击之后。toggleTab里面的函数需要两个 对象 实例对象 this 当前 li this
            this.lis[i].onclick = this.toggleTab.bind(this, this.lis[i]); //this 实例对象
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
        //点击加号
        this.tabadd.onclick = this.addTadpole.bind(this);
    }
    //切换功能 方法方便调用
    toggleTab(that) {
        this.clearClass();
        //让当前 li添加 liactive
        that.className = 'liactive'
        //让对应索引号的section添加conactive
        this.sections[that.index].className = 'conactive'
    }
    //移除类名
    clearClass() {
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].className = '';
            this.sections[i].className = '';
        }
    }
    //2增加 功能 addTab()
    addTadpole() {
        this.clearClass();
        //此时的this指向  + 按钮
        //1 动态创建一个 li 然后插入父元素的 最后面
        var li = '<li class="liactive"><span>测试xx</span><span class="iconfont icon-guanbi"></span></li>';
        var section = ' <section class="conactive">测试xx</section>';
        this.ul.insertAdjacentHTML('beforeend', li);
        this.tabscon.insertAdjacentHTML('beforeend', section);
        this.init();
    }
    //修改功能 editTab()
    //this指向span
    editTab() {
        var str = this.innerHTML; //保留原来的文字、
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        //1双击span里面生成input
        this.innerHTML = '<input type="text" value=' + str + '>';
        var input = this.children[0];
        input.select(); //默认选中文本框里面的文字
        //文本框失去焦点,把文本框里面的值给span 文本 框不要了
        input.onblur = function () {
            this.parentNode.innerHTML = this.value;
        }
        input.onkeydown = function (event) {
            if (event.keyCodes == 13) {
                //何不自动调用失去焦点事件
                this.blur();
            }
        }
    }
}
new Tab('#tab');

相关标签: javaScript 笔记