JS實現(xiàn)動態(tài)增加和刪除li標(biāo)簽行的實例代碼
更新時間:2016年10月16日 09:41:10 投稿:jingxian
下面小編就為大家?guī)硪黄狫S實現(xiàn)動態(tài)增加和刪除li標(biāo)簽行的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
如下所示:
function addDepartment()
{
<span style="white-space:pre"> </span>var x = document.getElementById('department');
<span style="white-space:pre"> </span>var l = x.childNodes.length;
<span style="white-space:pre"> </span>var li = document.createElement("li");
<span style="white-space:pre"> </span>li.className = "list_nr_bg";
<span style="white-space:pre"> </span>li.innerHTML = "<span class='nr6'><select name='institute'><c:if test='${baseInstitutes !=null}'><c:forEach items='${baseInstitutes}' var='baseInstitute'><option value='${baseInstitute.id}'>${baseInstitute.instituteName}</option></c:forEach></c:if></select></span><span class='nr6'>專業(yè):</span><span class='nr6'><input name='specialty' type='text'></span><span><input type='button' onclick='deleteDepartment(this)' value='刪除'></span>";
<span style="white-space:pre"> </span>x.appendChild(li);
}
function deleteDepartment(obj)
{
<span style="white-space:pre"> </span>if(window.confirm("確認刪除嗎?")){
<span style="white-space:pre"> </span>var li = obj.parentNode.parentNode; //獲得當(dāng)前行
<span style="white-space:pre"> </span>var ul = li.parentNode;//獲得上一級標(biāo)簽
<span style="white-space:pre"> </span>ul.removeChild(li); //刪除當(dāng)前行
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
}
HTML:
<ul id="department">
<span style="white-space:pre"> </span><li class="list_nr_bg">
<span style="white-space:pre"> </span><span class="nr6">所屬院系:</span>
<span style="white-space:pre"> </span><span><input type="button" onclick="addDepartment()" value="增加"></span>
<span style="white-space:pre"> </span></li>
<span style="white-space:pre"> </span><li class="list_nr_bg">
<span style="white-space:pre"> </span><span class="nr6">
<span style="white-space:pre"> </span><select name="institute">
<span style="white-space:pre"> </span><c:if test="${baseInstitutes !=null}">
<span style="white-space:pre"> </span><c:forEach items="${baseInstitutes}" var="baseInstitute">
<span style="white-space:pre"> </span><option value="${baseInstitute.id}">${baseInstitute.instituteName}</option>
<span style="white-space:pre"> </span></c:forEach>
<span style="white-space:pre"> </span></c:if>
<span style="white-space:pre"> </span>/select>
<span style="white-space:pre"> </span></span>
<span style="white-space:pre"> </span><span class="nr6">專業(yè):</span>
<span style="white-space:pre"> </span><span class="nr6"><input name="specialty" type="text"></span>
<span style="white-space:pre"> </span></li></ul>
以上就是小編為大家?guī)淼腏S實現(xiàn)動態(tài)增加和刪除li標(biāo)簽行的實例代碼全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
非阻塞動態(tài)加載javascript廣告實現(xiàn)代碼
非阻塞動態(tài)加載javascript廣告,需要的朋友可以參考下。2010-11-11
教你用Uniapp實現(xiàn)微信小程序的GPS定位打卡
地圖組件用于展示地圖,而定位API只是獲取坐標(biāo),請勿混淆兩者,下面這篇文章主要給大家介紹了關(guān)于如何使用Uniapp實現(xiàn)微信小程序的GPS定位打卡的相關(guān)資料,需要的朋友可以參考下2022-11-11
Javascript實現(xiàn)頁面滾動時導(dǎo)航智能定位
本篇文章主要介紹了Javascript實現(xiàn)頁面滾動時導(dǎo)航智能定位,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
bootstrap tooltips在 angularJS中的使用方法
這篇文章主要介紹了bootstrap tooltips在 angularJS中的使用 ,需要的朋友可以參考下2019-04-04

