html怎么复制一行,js实现复制,删除一行div或一行table

在网上看到一段通过对节点的控制增加或删除一行table的代码如下:

无标题文档

//给table增加一行

function addTableRow() {

var table1 = document.getElementById('table1');

var cloneTab = document.getElementById('table2');

//alert(cloneTab.firstChild.firstChild.innerHTML);

//alert(cloneTab.firstChild.firstChild.cloneNode(true).innerHTML);

table1.firstChild.appendChild(cloneTab.firstChild.firstChild.cloneNode(true));

var v= table1.firstChild.childNodes;

var len = v.length;

for(var i=1;i

v[i].childNodes[0].firstChild.id=i;//给第一个单元格id赋值

}

}

//给table删除一行

function delTableRow(){

var table1 = document.getElementById('table1');

var isChecked = document.getElementsByName('isChecked');

var len = isChecked.length;

for(var i=len-1;i>=0;i--){

if(isChecked[i].checked==true){

table1.firstChild.removeChild(isChecked[i].parentNode.parentNode);

//alert(isChecked[i].id);

//alert(isChecked[i].parentNode.parentNode.innerHTML);

}

}

}

000123456

请选择...

1

1

因为我用的是div+css,所以将上面的代码改成如下:

无标题文档

.box_quantity_box{ width:250px;

border:1px solid #7a4e25;

font-size:12px;

}

.box_quantity_top{background:#7a4e25;

color:#fefdfb;

width:100%;

height:30px;

}

.box_quantity_con{width:100%;

background:#fef0e5;

height:50px;

}

.box_quantity_ul{ list-style:none;

margin:0px;

padding:0px;

clear:both;

}

.box_quantity_li1{ float:left;

width:60px;

margin:8px 0px 0px 0px;

padding:0px;

text-align:center;

}

.box_quantity_li2{ float:left;

width:30px;

margin:8px 0px 0px 0px;

padding:0px;

text-align:center;

}

.txt1{width:55px;

background:#fef0e5;

border:1px solid #7a4e25;

height:15px;

font-size:12px;

color:#7a4e25;

}

.selecsty1{ width:50px;

background:#fef0e5;

border:1px solid #7a4e25;

height:15px;

font-size:12px;

color:#7a4e25;

}

.singlesty{background:#fef0e5;

color:#7a4e25;

}

.close{clear:both;}

.box_quantity_button {

border:1px solid #7a4e25;

width:47px;

height:17px;

background:#fefdfb;

color:#7a4e25;

cursor:pointer;

}

//给table增加一行

function addTableRow() {

var table1 = document.getElementById('table1');

var cloneTab = document.getElementById('table2');

alert(cloneTab.firstChild.firstChild.innerHTML);

alert(cloneTab.firstChild.firstChild.cloneNode(true).innerHTML);

/* table1.firstChild.appendChild(cloneTab.firstChild.firstChild.cloneNode(true));   */

table1.firstChild.appendChild(cloneTab.firstChild.cloneNode(true));

var v= table1.firstChild.childNodes;

var len = v.length;

for(var i=1;i

v[i].childNodes[0].firstChild.id=i;//给第一个单元格id赋值

}

}

//给table删除一行

function delTableRow(){

var table1 = document.getElementById('table1');

var isChecked = document.getElementsByName('isChecked');

var len = isChecked.length;

for(var i=len-1;i>=0;i--){

if(isChecked[i].checked==true){

table1.firstChild.removeChild(isChecked[i].parentNode.parentNode);

//alert(isChecked[i].id);

//alert(isChecked[i].parentNode.parentNode.innerHTML);

}

}

}

  •  操作
  • 哈哈1
  • 哈哈2
  • 哈哈3
  • 哈哈4

新增 

删除 

  • 呵呵

    哈哈

    啦啦

  • 1

    2

    3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值