實作功能:
通常在編輯表格時表格的行數是不確定的,如果一次增加太多行可能導致頁面內容太多,反應變慢;透過此程式實現表格動態增加行,一直保持最下面有多個空白行。
效果:
一:原頁
二:表1增加新行並綁定timepicker
三:表2自動增加行,新行綁定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" /> //为表格主体添加单击事件,当单击时添加行数,使表格保持有n个空行 (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]; } } //添加一个隐藏行,后面新增行复制此行 var lastRow = tb.rows[tb.rows.length - 1]; var row = $(lastRow).clone(true, true); $(row).insertAfter($(tb).find("tr:last")).hide(); //为除所有行添加事件,当获得焦点时自动增加新行 for (var i = 0; i < tb.rows.length; i++) { AddAutoRowsEvent(tb.rows[i]); } }); } }); //自动增加行 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]); //如果有回调函数则执行 if (typeof (rowfunction) == 'function') { rowfunction(row); } } } } //为指定行增加事件 function AddAutoRowsEvent(tr) { //如果是JQuery对象则转为HTML对象 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); } } } } //在表格中指定位置插入指定行数,新插入的行内容为同一表格主体最后一行 //obj:行内的任意对象 //n:要增加的行数 //bAutoRows:是否要添加自动增加行的属性 $.fn.tableAutoRow.insertRow = function (obj, n, bAutoRows, isInsertAfter) { var loop = 0; //加入循环次数,防止死循环 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); //将新行添加到当前行之前/后 if (isInsertAfter) { row.insertAfter(obj).show(); } else { row.insertBefore(obj).show(); } if (bAutoRows) { AddAutoRowsEvent(row); } } } //清除指定行数据 //obj为行或者行内的节点 //startColnum:起始列 //endColumn:终止列 //isReset:是否恢复到初始值 $.fn.tableAutoRow.clearRowData = function (obj, startColnum, endColumn, isReset) { var loop = 0; //加入循环次数,防止死循环 while (obj.tagName != "TR" && loop < 10) { obj = obj.parentNode; loop++; } if (obj.tagName != "TR") { return; } var cellsCount = obj.cells.length; //此行单元格总数 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++) //循环各列,设置单元格里的控件值 { for (var j = 0; j < obj.cells[c].childNodes.length; j++) { //循环处理指定单元格中的子节点 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);

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

本教程向您展示瞭如何將自定義的Google搜索API集成到您的博客或網站中,提供了比標準WordPress主題搜索功能更精緻的搜索體驗。 令人驚訝的是簡單!您將能夠將搜索限制為Y

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

本文系列在2017年中期進行了最新信息和新示例。 在此JSON示例中,我們將研究如何使用JSON格式將簡單值存儲在文件中。 使用鍵值對符號,我們可以存儲任何類型的

增強您的代碼演示文稿:10個語法熒光筆針對開發人員在您的網站或博客上共享代碼段的開發人員是開發人員的常見實踐。 選擇合適的語法熒光筆可以顯著提高可讀性和視覺吸引力。 t

利用輕鬆的網頁佈局:8 ESTISSEL插件jQuery大大簡化了網頁佈局。 本文重點介紹了簡化該過程的八個功能強大的JQuery插件,對於手動網站創建特別有用

本文介紹了關於JavaScript和JQuery模型視圖控制器(MVC)框架的10多個教程的精選選擇,非常適合在新的一年中提高您的網絡開發技能。 這些教程涵蓋了來自Foundatio的一系列主題

核心要點 JavaScript 中的 this 通常指代“擁有”該方法的對象,但具體取決於函數的調用方式。 沒有當前對象時,this 指代全局對象。在 Web 瀏覽器中,它由 window 表示。 調用函數時,this 保持全局對象;但調用對象構造函數或其任何方法時,this 指代對象的實例。 可以使用 call()、apply() 和 bind() 等方法更改 this 的上下文。這些方法使用給定的 this 值和參數調用函數。 JavaScript 是一門優秀的編程語言。幾年前,這句話可


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版
視覺化網頁開發工具