欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery列表動(dòng)態(tài)增加和刪除的實(shí)現(xiàn)方法

 更新時(shí)間:2020年11月05日 10:33:13   作者:jiangrunkang.  
這篇文章主要給大家介紹了關(guān)于jQuery列表動(dòng)態(tài)增加和刪除的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

通過(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>&nbsp<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>&nbsp<a href="#">刪除</a></td>
 </tr>
 <tr>
  <td><input type="checkbox"/></td>
  <td>2</td>
  <td>2</td>
  <td>2</td>
  <td><a href="#">修改</a>&nbsp<a href="#">刪除</a></td>
 </tr>
 <tr>
  <td><input type="checkbox"/></td>
  <td>3</td>
  <td>3</td>
  <td>3</td>
  <td><a href="#">修改</a>&nbsp<a href="#">刪除</a></td>
 </tr>
 <tr>
  <td><input type="checkbox"/></td>
  <td>4</td>
  <td>4</td>
  <td>4</td>
  <td><a href="#">修改</a>&nbsp<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>&nbsp<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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論