jQuery列表動(dòng)態(tài)增加和刪除的實(shí)現(xiàn)方法
通過(guò)jQuery實(shí)現(xiàn)列表的數(shù)據(jù)動(dòng)態(tài)添加與刪除
代碼演示
演示地址
背景隔行換色
CSS代碼
/* 設(shè)置奇數(shù)行顏色 */ .even { background-color: dodgerblue; } /* 設(shè)置偶數(shù)行背景顏色 */ .odd { background-color: pink; }
JavaScript代碼
// 設(shè)置奇數(shù)偶數(shù)行的背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd");
全選和取消全選事件
//設(shè)置全選和取消全選事件 $("thead th:first").append("<span id='show'></span>") $("thead input:checkbox").click(function () { if ($(this).prop("checked")) { $("tbody input:checkbox").prop("checked", true); $("#show").replaceWith("<span id='show'>已全選</span>"); } else { $("tbody input:checkbox").prop("checked", false); $("#show").replaceWith("<span id='show'>已取消全選</span>"); } });
刪除
//給當(dāng)前和未來(lái)元素添加綁定事件 $("tbody").on("click", "td>a:nth-child(2)", function () { if (confirm("確定要?jiǎng)h除嗎?")) { //取消背景顏色 $("tr:even").removeClass("even"); $("tr:odd").removeClass("odd"); //移除tr $(this).parent().parent().remove() //添加背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd"); } });
新增
HTML代碼
<!--新增列表--> <div> <form> <table> <tr> <td>序號(hào)</td> <td><input type="text"/></td> </tr> <tr> <td>分類名</td> <td><input type="text"/></td> </tr> <tr> <td>分類描述</td> <td><input type="text"/></td> </tr> <input type="reset" value="確定"/> <input type="reset" value="取消"/> </table> </form> </div>
CSS代碼
/*默認(rèn)隱藏新增頁(yè)面*/ div:nth-child(2) { display: none; }
JavaScript代碼
// 點(diǎn)擊"新增"按鈕顯示新增列表 $("div:nth-child(1)>input:button").click(function () { $("div:nth-child(2)").show("slow"); });
//點(diǎn)擊"取消"按鈕隱藏div $("div:nth-child(2) :reset:last").click(function () { $("div:nth-child(2)").hide("slow"); });
//點(diǎn)擊"確定"按鈕,提交表單 $("div:nth-child(2) :reset:first-child").click(function () { $("div:nth-child(1) tbody").append(" <tr>\n" + " <td><input type=\"checkbox\"/></td>\n" + " <td></td>\n" + " <td></td>\n" + " <td></td>\n" + " <td><a href=\"#\">修改</a> <a href=\"#\">刪除</a></td>\n" + " </tr>"); // 獲取文本框節(jié)點(diǎn) var textDom = $("div:nth-child(2) :text"); // 獲取td節(jié)點(diǎn) var tdDom = $("div:nth-child(1) tr:last td"); for (var i = 0; i < textDom.length; i++) { //獲取文本框內(nèi)容 var content = textDom.eq(i).val(); // 把文本框獲取的內(nèi)容寫(xiě)入到td內(nèi) tdDom.eq(i + 1).text(content); } //添加背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd"); $("div:nth-child(2)").hide("slow"); });
完整代碼
HTML代碼
<body> <div> <input type="button" value="新增"/> <table cellpadding="0px" cellspacing="0px"> <thead> <tr> <th><input type="checkbox"/></th> <th>序號(hào)</th> <th>分類名</th> <th>分類描述</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox"/></td> <td>1</td> <td>1</td> <td>1</td> <td><a href="#">修改</a> <a href="#">刪除</a></td> </tr> <tr> <td><input type="checkbox"/></td> <td>2</td> <td>2</td> <td>2</td> <td><a href="#">修改</a> <a href="#">刪除</a></td> </tr> <tr> <td><input type="checkbox"/></td> <td>3</td> <td>3</td> <td>3</td> <td><a href="#">修改</a> <a href="#">刪除</a></td> </tr> <tr> <td><input type="checkbox"/></td> <td>4</td> <td>4</td> <td>4</td> <td><a href="#">修改</a> <a href="#">刪除</a></td> </tr> </tbody> </table> </div> <!--新增列表--> <div> <form> <table> <tr> <td>序號(hào)</td> <td><input type="text"/></td> </tr> <tr> <td>分類名</td> <td><input type="text"/></td> </tr> <tr> <td>分類描述</td> <td><input type="text"/></td> </tr> <input type="reset" value="確定"/> <input type="reset" value="取消"/> </table> </form> </div> </body>
CSS代碼
table, th, td { border: 1px solid black; } th, td { width: 120px; height: 40px; line-height: 40px; text-align: center; } /* 設(shè)置奇數(shù)行顏色 */ .even { background-color: dodgerblue; } /* 設(shè)置偶數(shù)行背景顏色 */ .odd { background-color: pink; } /* 默認(rèn)隱藏新增頁(yè)面*/ div:nth-child(2) { display: none; }
JavaScript代碼
<script> $(function () { // 設(shè)置奇數(shù)偶數(shù)行的背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd"); //設(shè)置全選和取消全選事件 $("thead th:first").append("<span id='show'></span>") $("thead input:checkbox").click(function () { if ($(this).prop("checked")) { $("tbody input:checkbox").prop("checked", true); $("#show").replaceWith("<span id='show'>已全選</span>"); } else { $("tbody input:checkbox").prop("checked", false); $("#show").replaceWith("<span id='show'>已取消全選</span>"); } }); //新增商品 $("div:nth-child(1)>input:button").click(function () { $("div:nth-child(2)").show("slow"); }); //"取消" 按鈕 $("div:nth-child(2) :reset:last").click(function () { $("div:nth-child(2)").hide("slow"); }); //"確定" 按鈕 $("div:nth-child(2) :reset:first-child").click(function () { $("div:nth-child(1) tbody").append(" <tr>\n" + " <td><input type=\"checkbox\"/></td>\n" + " <td></td>\n" + " <td></td>\n" + " <td></td>\n" + " <td><a href=\"#\">修改</a> <a href=\"#\">刪除</a></td>\n" + " </tr>"); // 獲取文本框節(jié)點(diǎn) var textDom = $("div:nth-child(2) :text"); // 獲取td節(jié)點(diǎn) var tdDom = $("div:nth-child(1) tr:last td"); for (var i = 0; i < textDom.length; i++) { //獲取文本框內(nèi)容 var content = textDom.eq(i).val(); // 把文本框獲取的內(nèi)容寫(xiě)入到td內(nèi) tdDom.eq(i + 1).text(content); } //添加背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd"); $("div:nth-child(2)").hide("slow"); }); //給當(dāng)前和未來(lái)元素添加綁定事件 $("tbody").on("click", "td>a:nth-child(2)", function () { if (confirm("確定要?jiǎng)h除嗎?")) { //取消背景顏色 $("tr:even").removeClass("even"); $("tr:odd").removeClass("odd"); //移除tr $(this).parent().parent().remove() //添加背景顏色 $("tr:even").addClass("even"); $("tr:odd").addClass("odd"); } }); }); </script>
總結(jié)
到此這篇關(guān)于jQuery列表動(dòng)態(tài)增加和刪除實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)jQuery列表動(dòng)態(tài)增加和刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- jQuery實(shí)現(xiàn)動(dòng)態(tài)顯示select下拉列表數(shù)據(jù)的方法
- jQuery實(shí)現(xiàn)動(dòng)態(tài)加載select下拉列表項(xiàng)功能示例
- jQuery實(shí)現(xiàn)列表內(nèi)容的動(dòng)態(tài)載入特效
- 基于jQuery和Bootstrap框架實(shí)現(xiàn)仿知乎前端動(dòng)態(tài)列表效果
- jQuery動(dòng)態(tài)產(chǎn)生select option下拉列表
- jQuery簡(jiǎn)單實(shí)現(xiàn)向列表動(dòng)態(tài)添加新元素的方法示例
- jQuery實(shí)現(xiàn)動(dòng)態(tài)生成年月日級(jí)聯(lián)下拉列表示例
- 如何使用Jquery動(dòng)態(tài)生成二級(jí)選項(xiàng)列表
相關(guān)文章
jQuery EasyUI API 中文文檔 - ComboTree組合樹(shù)
jQuery EasyUI API 中文文檔 - ComboTree組合樹(shù),需要的朋友可以參考下。2011-10-10jquery插件實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)圖片右側(cè)顯示大圖的效果(類似淘寶)
分享一個(gè)jquery插件:實(shí)現(xiàn)類似淘寶上鼠標(biāo)經(jīng)過(guò)圖片右側(cè)顯示大圖的效果,感興趣的朋友可以研究下,或許對(duì)你學(xué)習(xí)jquery有所幫助,千萬(wàn)不要錯(cuò)過(guò)啊2013-02-02jquery struts 驗(yàn)證唯一標(biāo)識(shí)(公用方法)
本教程將為大家詳細(xì)介紹下使用公用方法驗(yàn)證jquery struts唯一標(biāo)識(shí),感興趣的朋友可以參考下哈,希望可以幫助到你2013-03-03jQuery中innerHeight()方法用法實(shí)例
這篇文章主要介紹了jQuery中innerHeight()方法用法,實(shí)例分析了innerHeight()方法的功能、定義及獲取第一個(gè)匹配元素內(nèi)部區(qū)域高度的使用技巧,需要的朋友可以參考下2015-01-01Jquery實(shí)現(xiàn)瀑布流布局(備有詳細(xì)注釋)
這篇文章主要介紹了Jquery實(shí)現(xiàn)瀑布流布局的方法,可實(shí)現(xiàn)圖片的動(dòng)態(tài)加載功能,且代碼備有詳細(xì)注釋便于理解,需要的朋友可以參考下2015-07-07JQuery報(bào)錯(cuò)Uncaught TypeError: Illegal invocation的處理方法
這篇文章主要介紹了JQuery報(bào)錯(cuò)"Uncaught TypeError: Illegal invocation"的處理方法,需要的朋友可以參考下2015-03-03