麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

JQuery實(shí)現(xiàn)表格動(dòng)態(tài)增加行并對(duì)新行添加事件

2019-11-20 14:18:39
字體:
供稿:網(wǎng)友

實(shí)現(xiàn)功能:

通常在編輯表格時(shí)表格的行數(shù)是不確定的,如果一次增加太多行可能導(dǎo)致頁面內(nèi)容太多,反應(yīng)變慢;通過此程序?qū)崿F(xiàn)表格動(dòng)態(tài)增加行,一直保持最下面有多個(gè)空白行。

效果:

一:原始頁面

二:表1增加新行并綁定timepicker

三:表2自動(dòng)增加行,新行綁定timepicker

HTML源碼:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <link href="../Script/jquery-easyui-1.3.2/themes/default/easyui.css" rel="external nofollow" rel="stylesheet" /> <style> .autoRows{ width: 350px; border:1px green solid; } .autoRows tbody tr td{ border-bottom:1px green solid; margin:0px; } .autoRows thead{ background-color:#8ec7d7; } .autoRows tfoot { background-color: #8ec7d7; } </style> </head> <body> <table border="0" cellspacing="0" id="table1" class="autoRows"> <thead> <tr> <th>表頭1</th> <th>表頭1</th> <th>表頭1</th> </tr> <tr> <th>表頭2</th> <th>表頭2</th> <th>表頭2</th> </tr> </thead> <tbody> <tr> <td> <input id="Button1" type="button" value="insertAfter" onclick="addrow(this);" /></td> <td> <input id="Button3" type="button" value="Clear" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, false);" /></td> <td> <input id="Text2" type="text" value="aaaa" /></td> </tr> <tr> <td> <input id="Button2" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this,1,true,false);" /></td> <td> <input id="Button4" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> <td> <input id="Text1" name="ttt" type="text" value="asdfasfasfdsd" /></td> </tr> <tr> <td> <input id="Button5" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this,1,true,false);" /></td> <td> <input id="Button6" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> <td> <input id="Text3" type="text" name="Text3" /></td> </tr> </tbody> <tfoot> <tr> <th>表尾1</th> <th>表尾2</th> <th>表尾3</th> </tr> </tfoot> </table> <div style="height:20px;"></div> <table border="0" cellspacing="0" id="table2" class="autoRows"> <thead> <tr> <th>表頭1</th> <th>表頭1</th> <th>表頭1</th> </tr> <tr> <th>表頭2</th> <th>表頭2</th> <th>表頭2</th> </tr> </thead> <tbody> <tr> <td> <input id="Button7" type="button" value="insertAfter" onclick="addrow(this);" /></td> <td> <input id="Button8" type="button" value="Clear" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, false);" /></td> <td> <input id="Text4" type="text" value="aaaa" /></td> </tr> <tr> <td> <input id="Button9" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this, 1, true, false);" /></td> <td> <input id="Button10" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> <td> <input id="Text5" name="ttt" type="text" value="asdfasfasfdsd" /></td> </tr> <tr> <td> <input id="Button11" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this, 1, true, false);" /></td> <td> <input id="Button12" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> <td> <input id="Text6" type="text" name="Text3" /></td> </tr> </tbody> <tfoot> <tr> <th>表尾1</th> <th>表尾2</th> <th>表尾3</th> </tr> </tfoot> </table> </body> </html> <script src="../Script/jquery-1.7.2.min.js"></script> <script src="../Script/jquery.tableAutoRow.js"></script> <script src="../Script/jquery-easyui-1.3.2/jquery.easyui.min.js"></script> <script src="../Script/jquery.timepicker.js"></script> <script type="text/javascript"> $(function () { $(".autoRows").tableAutoRow(aaa); function aaa(row) { $(row).find(':text').timepicker(); } }); function addrow(obj) { $.fn.tableAutoRow.insertRow(obj); } </script>

JS源碼:

/// <reference path="jquery-1.7.2.min.js" /> //為表格主體添加單擊事件,當(dāng)單擊時(shí)添加行數(shù),使表格保持有n個(gè)空行 (function ($) { $.fn.extend({ rowfunction: null, tableAutoRow: function (newRowFunction) { rowfunction = newRowFunction; return $(this).each(function () { var tb = this; if (!(this.tagName.toUpperCase() == "TBODY")) { if (!this.tBodies[0]) { return; } else { tb = this.tBodies[0]; } } //添加一個(gè)隱藏行,后面新增行復(fù)制此行 var lastRow = tb.rows[tb.rows.length - 1]; var row = $(lastRow).clone(true, true); $(row).insertAfter($(tb).find("tr:last")).hide(); //為除所有行添加事件,當(dāng)獲得焦點(diǎn)時(shí)自動(dòng)增加新行 for (var i = 0; i < tb.rows.length; i++) { AddAutoRowsEvent(tb.rows[i]); } }); } }); //自動(dòng)增加行 function autoRows(e) { var e = e || event; var obj = e.target || e.srcElement; while (obj.tagName != "TR") { obj = obj.parentNode; } var tb = obj.parentNode; var index = $(obj).index(); var n = 5 - (tb.rows.length - index); if (n > 0) { var lastRow = tb.rows[tb.rows.length - 1]; for (var j = 0; j < n; j++) { var row = $(lastRow).clone(true, true); //將新行添加到最后一行之前 row.insertBefore($(tb).find("tr:last")).show(); //為新增加的行添加事件 //AddAutoRowsEvent(tb.rows[tb.rows.length - 2]); //如果有回調(diào)函數(shù)則執(zhí)行 if (typeof (rowfunction) == 'function') { rowfunction(row); } } } } //為指定行增加事件 function AddAutoRowsEvent(tr) { //如果是JQuery對(duì)象則轉(zhuǎn)為HTML對(duì)象 if (tr instanceof jQuery) { tr = tr[0]; } $(tr).bind('click', autoRows); var c = tr.cells.length; for (var j = 0; j < c; j++) { var childs = tr.cells[j].childNodes; for (var k = 0; k < childs.length; k++) { if (childs[k].type == "text" || childs[k].type == "textarea" || childs[k].type == "button") { $(childs[k]).bind('focus', autoRows); } } } } //在表格中指定位置插入指定行數(shù),新插入的行內(nèi)容為同一表格主體最后一行 //obj:行內(nèi)的任意對(duì)象 //n:要增加的行數(shù) //bAutoRows:是否要添加自動(dòng)增加行的屬性 $.fn.tableAutoRow.insertRow = function (obj, n, bAutoRows, isInsertAfter) { var loop = 0; //加入循環(huán)次數(shù),防止死循環(huán) while (obj.tagName != "TR" && loop < 10) { obj = obj.parentNode; loop++; } if (obj.tagName != "TR") { return; } var tb = obj.parentNode; switch (arguments.length) { case 3: var isInsertAfter = true; case 2: var bAutoRows = true; var isInsertAfter = true; case 1: var bAutoRows = true; var isInsertAfter = true; var n = 1; } for (var i = 0; i < n; i++) { var lastRow = tb.rows[tb.rows.length - 1]; var row = $(lastRow).clone(true, true); //將新行添加到當(dāng)前行之前/后 if (isInsertAfter) { row.insertAfter(obj).show(); } else { row.insertBefore(obj).show(); } if (bAutoRows) { AddAutoRowsEvent(row); } } } //清除指定行數(shù)據(jù) //obj為行或者行內(nèi)的節(jié)點(diǎn) //startColnum:起始列 //endColumn:終止列 //isReset:是否恢復(fù)到初始值 $.fn.tableAutoRow.clearRowData = function (obj, startColnum, endColumn, isReset) { var loop = 0; //加入循環(huán)次數(shù),防止死循環(huán) while (obj.tagName != "TR" && loop < 10) { obj = obj.parentNode; loop++; } if (obj.tagName != "TR") { return; } var cellsCount = obj.cells.length; //此行單元格總數(shù) if (startColnum < 0 || !startColnum) { //如果未指定清除起始列則從第一列清除 startColnum = 0; } if (endColumn > cellsCount - 1 || !endColumn) { //如果未指定清除終止列則清除到最后一列前(通常最后一列用于放置清除按鈕) endColumn = cellsCount - 1; } if (isReset == undefined) { isReset = false; } for (var c = startColnum; c <= endColumn; c++) //循環(huán)各列,設(shè)置單元格里的控件值 { for (var j = 0; j < obj.cells[c].childNodes.length; j++) { //循環(huán)處理指定單元格中的子節(jié)點(diǎn) var node = obj.cells[c].childNodes[j]; setObjData(node, isReset); } } }; function setObjData(node, isReset) { switch (node.type) { case "text": case "hidden": case "textarea": if (isReset) { node.value = node.defaultValue; } else { node.value = ""; } break; case "select-one": case "select-multiple": if (isReset) { for (var k = node.options.length - 1; k >= 0; k--) { node.options[k].selected = node.options[k].defaultSelected; } } else { for (var k = node.options.length - 1; k >= 0; k--) { //node.options.remove(k); node.options[k].selected = false; } } break; case "checkbox": case "radio": if (isReset) { node.checked = node.defaultChecked; } else { node.checked = false; } break; } if (node.childNodes && node.childNodes.length > 0) { var l = node.childNodes.length; for (var i = 0; i < l; i++) { setObjData(node.childNodes[i], isReset); } } } })(jQuery);
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 91精品一区二区综合在线 | 一级片a | 91九色视频观看 | www国产成人免费观看视频,深夜成人网 | 日韩黄色一级视频 | 日本在线一区二区 | 视频二区国产 | 国内久久久久 | 国产亚洲精品精 | a黄色片| 亚洲一区在线免费视频 | 国产精品视频亚洲 | 在线天堂中文在线资源网 | 午夜精品小视频 | 一区二区三区视频在线观看 | 成人电影毛片 | 天天色综合2 | 一区二区久久久久草草 | 一级做a爱片性色毛片高清 日本一区二区在线看 | 影视免费观看 | 国产一级二级视频 | 一区在线视频观看 | 精品国产看高清国产毛片 | 主播粉嫩国产在线精品 | 成人wxx视频免费 | 成人男女啪啪免费观看网站四虎 | 国产精品99一区二区 | 88xx成人精品视频 | 久久久久电影网站 | 国产88久久久国产精品免费二区 | 国产视频导航 | 成人午夜在线免费 | 乱淫67194| 男女一边摸一边做羞羞视频免费 | 草草视频免费 | 91九色电影 | av国产免费| 天天草天天爱 | 免费a级黄色片 | 九九黄色| 国产一区二区三区网站 |