原生js删除html,比较完整的原生js选项卡,有添加和删除功能

这篇博客详细介绍了如何使用CSS和JavaScript来创建一个动态选项卡组件。该组件包括可点击的选项卡导航,自动调整宽度以适应内容,以及添加、删除和编辑选项卡的功能。通过双击可以编辑选项卡的文本,并且当选项卡被删除时,能够自动切换到下一个可用的选项卡。此外,还展示了如何通过JavaScript的事件监听和DOM操作来实现这些交互效果。
摘要由CSDN通过智能技术生成

css部分

* {

margin: 0;

padding: 0;

list-style: none;

}

body {

text-align: center;

}

.tabsbox {

width: 900px;

margin: 50px auto;

}

.firstnav {

border: #ddd 1px solid;

height: 50px;

width: 100%;

overflow: hidden;

position: relative;

line-height: 50px;

}

.firstnav ul {

position: absolute;

left: 0;

top: 0px;

}

.firstnav li {

border-right: #ddd 1px solid;

float: left;

cursor: pointer;

box-sizing: border-box;

padding: 0 20px;

position: relative;

height: 50px;

}

.firstnav li input {

width: 110px;

height: 30px;

}

.firstnav li.liactive {

background-color: #f0f0f0;

}

.close {

cursor: pointer;

position: absolute;

right: 0;

top: 5px;

display: inline-block;

width: 10px;

height: 2px;

background: #f00;

line-height: 0;

font-size: 0;

vertical-align: middle;

transform: rotate(45deg);

}

.close:after {

content: '/';

display: block;

width: 10px;

height: 2px;

background: #f00;

transform: rotate(-90deg);

}

.tabadd {

position: absolute;

right: 0;

top: 0;

width: 40px;

background-color: #999;

color: #fff;

}

.tabscon {

border: #ddd 1px solid;

border-top: 0;

}

.tabscon section {

height: 500px;

display: none;

padding: 25px;

}

.tabscon section input {

width: 700px;

height: 400px;

}

.tabscon section.conactive {

display: block

}

HTML部分

  • 测试1 删除

  • 测试2 删除

  • 测试3 删除

+

test1

test2

test3

javascript部分

var that;

class tab {

constructor(id) {

that = this;

this.main = document.querySelector(id);

this.width = this.main.offsetWidth;

this.add = this.main.querySelector('.tabadd');

this.tabscon = this.main.querySelector('.tabscon');

this.ul = this.main.querySelector('ul');

this.init();

}

init() {

this.updateNode();

this.add.onclick = this.addTab;

var ulWidth = 0;

for (var i = 0; i < this.lis.length; i++) {

ulWidth += Math.ceil(this.lis[i].offsetWidth + 1);

this.lis[i].index = i;

this.lis[i].onclick = this.toggleTab;

this.remove[i].onclick = this.removeTab;

this.spans[i].ondblclick = this.editTab;

this.sections[i].ondblclick = this.editTab;

}

this.ul.style.width = ulWidth + 'px';

if (ulWidth > this.width) {

this.ul.style.marginLeft = -(ulWidth - this.width + 40) + "px";

} else {

this.ul.style.marginLeft = 0 + "px";

}

}

updateNode() {

this.lis = this.main.querySelectorAll('li');

this.sections = this.main.querySelectorAll('section');

this.remove = this.main.querySelectorAll(".close");

this.spans = this.main.querySelectorAll('.firstnav li span:first-child');

}

toggleTab() {

that.clearClass();

this.className = 'liactive';

that.sections[this.index].className = 'conactive'

}

clearClass() {

for (var i = 0; i < this.lis.length; i++) {

this.lis[i].className = '';

this.sections[i].className = '';

}

}

addTab() {

that.clearClass();

var random = Math.random();

var li = '

新选项卡 删除';

var section = 'test1' + random + '';

that.ul.insertAdjacentHTML('beforeend', li);

that.tabscon.insertAdjacentHTML('beforeend', section);

that.init();

}

removeTab(e) {

e.stopPropagation();

var index = this.parentNode.index;

that.lis[index].remove();

that.sections[index].remove();

that.init();

if (document.querySelector('.liactive')) return;

index--;

that.lis[index] && that.lis[index].click();

}

editTab() {

//双击禁止选中文字

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();

var str = this.innerHTML;

this.innerHTML = ''

var input = this.children[0];

input.value = str;

input.select();

input.onblur = function () {

this.parentNode.innerHTML = this.value;

}

input.onkeyup = function (e) {

if (e.keyCode === 13) {

this.blur();

}

}

}

}

var tabsone = new tab("#tab");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值