87,901
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="outer_text">
<input id="con_value">
<button class="btn">增加</button>
<button class="btn">拷贝</button>
<button class="btn">删除</button>
<div id="inner_text"></div>
</div>
</body>
<script>
var inp_out = document.getElementById("con_value");
var btn_cla = document.getElementsByClassName("btn");
var div_inr = document.getElementById("inner_text");
function addText(){
var op = document.createElement('p');
var ob = document.createElement('button');
ob.innerHTML = 'x';
ob.setAttribute('class','btn2');
op.innerHTML = inp_out.value;
div_inr.appendChild(op);
op.appendChild(ob);
inp_out.value = '';
}
function removeBtn2(){
var op2 = this.parentNode;
op2.parentNode.removeChild();
}
btn_cla[0].onclick = addText;
var p1 = document.getElementsByTagName('p');
//var b1 = p1.getElementsByClassName('btn2');
for(var i=0;i<p1.length;i++){
p1[i].getElementsByClassName('btn2')[0].onclick = function(){
//this.parentNode.removeChild(this.parentNode)
console.log(this)
}
}
</script>
</html>
<script>
var inp_out = document.getElementById("con_value");
var btn_cla = document.getElementsByClassName("btn");
var div_inr = document.getElementById("inner_text");
function addText(){
var op = document.createElement('p');
var ob = document.createElement('button');
ob.innerHTML = 'x';
ob.setAttribute('class','btn2');
op.innerHTML = inp_out.value;
div_inr.appendChild(op);
op.appendChild(ob);
inp_out.value = '';
ob.onclick = function(){
div_inr.removeChild(this.parentNode);
}
}
function removeBtn2(){
var op2 = this.parentNode;
op2.parentNode.removeChild();
}
btn_cla[0].onclick = addText;
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="outer_text">
<input id="con_value">
<button class="btn">增加</button>
<button class="btn">拷贝</button>
<button class="btn">删除</button>
<div id="inner_text"></div>
</div>
</body>
<script>
var inp_out = document.getElementById("con_value");
var btn_cla = document.getElementsByClassName("btn");
var div_inr = document.getElementById("inner_text");
function addText(){
var op = document.createElement('p');
var ob = document.createElement('button');
ob.innerHTML = 'x';
ob.setAttribute('class','btn2');
op.innerHTML = inp_out.value;
div_inr.appendChild(op);
op.appendChild(ob);
inp_out.value = '';
ob.onclick = function() { // <====注册事件添加在这里
ob.closest('p').remove();
}
}
function removeBtn2(){
var op2 = this.parentNode;
op2.parentNode.removeChild();
}
btn_cla[0].onclick = addText;
</script>
</html>