jQuery+JavaScript 实现 table 的增加和减少
2012-05-02 11:27
169 查看
<!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>jQuery+JavaScript 实现 table 的增加和减少</title> <script language="javascript" src="jquery-1.7.1.min.js"></script> </head> <body> <table border="0" width="800" style="border-collapse:collapse;" id="tb1"> <tr> <td><input type="file" name="pic[]" /></td> </tr> </table> <input type="button" id="add" value="+" /> <input type="button" id="minus" value="-" /> <input type="button" id="empty" value="清空" /> <script type="text/javascript"> $("#add").click(function(){ $("#tb1").append('<tr><td><input type="file" name="pic[]" /></td></tr>'); }) $("#minus").click(function(){ var tbl_rows = Math.round((document.getElementById('tb1').rows.length)); if(tbl_rows>1) dealTableRows('tb1', 0, 1); }) $("#empty").click(function(){ $("#tb1").empty(); }) function dealTableRows(tbl_id, opt, cellnum, str){ var tblObj = document.getElementById(tbl_id); var newRow,newCell; switch(opt){ case 1:/* 增加一行 */ newRow = tblObj.insertRow(tblObj.rows.length); for(var i=0; i<cellnum; i++){ newCell = newRow.insertCell(newRow.cells.length); newCell.innerHTML = str[i]; } break ; default : for(var j=0;j<cellnum;j++){ tblObj.deleteRow(tblObj.rows.length-1); } break ; } } </script> </body> </html>
效果预览:
相关文章推荐
- javascript 实现动态增加table的行数
- ASP.NET中实现动态增加或减少TABLE列和行
- javascript 实现 table 增加行
- JavaScript 实现动态增加、删除表单域
- JavaScript实现下拉列表框数据增加、删除、上下排序的方法
- javascript 通用简单的table选项卡实现
- Javascript 实现table完全数据导出
- javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
- iOS table简单实现增加移动和删除功能与自定义cell的实现
- JavaScript和CSS通过expression实现Table居中显示
- 使用JAVASCRIPT实现动态增加、删除选择项
- javascript实现动态增加行删除行.
- Unity 3D实现血量增加减少
- 在javaScript中实现table列的新增和删除
- document.styleSheets以及js中实现样式表的切换、样式的增加减少
- 使用原生JavaScript实现对select增加option标签并附加value属性
- js动态控制table的tr、td增加及删除的具体实现
- JavaScript实现表中行的增加和删除!