博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS动态操作表格
阅读量:6994 次
发布时间:2019-06-27

本文共 2375 字,大约阅读时间需要 7 分钟。

1、web开发的项目中我们做条件查询数据添加附件上传等功能时候需要给予用户更多的动态选择,这样就不能将<input />框的个数写死,所以动态操作表格式一个不错的选择!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html 
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
>
<
meta 
http-equiv
=
"Content-Type" 
content
=
"text/html; charset=utf-8" 
/>
<
title
>无标题文档</
title
>
<
script 
type
=
"text/javascript" 
src
=
"jQuery/jquery-1.6.2.js"
></
script
>
<
script 
type
=
"text/javascript"
>
    
function AddSignRow(){
        
/*var str2=document.getElementById("trid").innerHTML;//这是通过js获取的一个tr中的所有元素
        
alert(str2);*/
         
var trObj="<
tr 
>"+$("#trid").html()+"</
tr
>";//通过jquery获取一个tr中所有的元素,然后拼成一个tr
        
var str=$("#tableid").find("tr").length;//获取table中tr总行数
        
if(str==11){//假如tr长度为11,也就是说,去掉第一个tr,还有10个tr的时候,停止增长,意思是最多只能新增10个tr
            
return;
        
}
         
$("#trid").after(trObj);//在这个<
tr
段落>之后插入一个HTML标记代码这是jquery外部插入,内部插入$("#tableid").append(trObj);   
    
}
    
function deleteRow(obj){
        
var str=$("#tableid").find("tr")[1];//获取table表中的所有tr,然后取第2个,因为是0,1,2,3
        
var tr=obj.parentNode.parentNode;  //获取obj代表是<
a
> 上一个父节点是<
td
> 再上就是<
tr
>
        
if(str==tr){ //加入获取的obj的再上父节点是第二个tr 不允许删除
            
return;
        
}
        
var tbody=tr.parentNode;   //获取tr的父节点
        
tbody.removeChild(tr);   //tr的父节点(tbody)移除tr
    
}
</
script
>
</
head
>
 
<
body
>
<
div 
class
=
"body_context" 
align
=
"center"
>
    
<
div 
class
=
"table_context"
>
        
<
table 
width
=
"80%" 
border
=
"1" 
cellspacing
=
"0" 
cellpadding
=
"0" 
id
=
"tableid"
>
           
<
thead
>
            
<
tr
>
                 
<
th
>姓名</
th
><
th
>照片</
th
><
th
>职业</
th
><
th
>操作</
th
>
            
</
tr
>
           
</
thead
>
            
<
tr 
id
=
"trid"
>
               
<
td
><
input 
type
=
"text" 
name
=
"" 
value
=
"" 
style
=
"border:0px;"
/></
td
>
               
<
td
><
input 
type
=
"file" 
name
=
"" 
value
=
"" 
style
=
"border:0px;"
/></
td
>
               
<
td
><
input 
type
=
"text" 
name
=
"" 
value
=
"" 
style
=
"border:0px;"
/></
td
>
               
<
td
><
a 
href
=
"#" 
onclick
=
"deleteRow(this)" 
style
=
"text-decoration:none;"
>移除</
a
></
td
>
            
</
tr
>
        
</
table
>
    
</
div
>
        
<
div 
style
=
"border-bottom: 1px solid #088000;margin-bottom: 10px;margin-top:10px;width:80%;" 
></
div
>
    
<
div 
style
=
"padding-left:0px;padding-top:5px;"
><
a 
onclick
=
"AddSignRow()" 
href
=
"javascript:void(0)" 
>创建</
a
></
div
>
</
div
>
</
body
>
</
html
>

本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1428201,如需转载请自行联系原作者
你可能感兴趣的文章
Javascript之JS盒子模型常用的属性、定时器、获取浏览器计算后的属性样式值
查看>>
改变世界的七大NLP技术,你了解多少?(下)
查看>>
通过机器学习和人工智能实现SDN
查看>>
阿里云API网关常见应用场景
查看>>
css和javascript在IE和Firefox中二十三个不同点
查看>>
android精品源码,下拉刷新效果高德地图五子棋游戏定制日历全民TV源码
查看>>
我的友情链接
查看>>
BTREE,mysql索引
查看>>
MySQL数据库开发必备常识
查看>>
不停止MySQL服务增加从库的两种方式
查看>>
Oracle 10g升级之--PSU 升级(续)
查看>>
通过案例学调优之--Oracle 全文索引
查看>>
迭代器和生成器原理
查看>>
ecshop显示商品的折扣 和节省的方法
查看>>
spring WebApplicationInitializer
查看>>
TURBOMAIL邮件服务器功能—邮件归档
查看>>
Code Review
查看>>
大规模服务中的问题小记
查看>>
ext2.0IE兼容chorme等浏览器
查看>>
【python语言程序设计】turtle库#180803
查看>>