JS实现动态增加和删除li标签行的实例代码

下面小编就为大家带来一篇JS实现动态增加和删除li标签行的实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

如下所示:

function addDepartment() { <span> </span>var x = document.getElementById('department'); <span> </span>var l = x.childNodes.length; <span> </span>var li = document.createElement("li"); <span> </span>li.className = "list_nr_bg"; <span> </span>li.innerHTML = "<span><select><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>专业:</span><span><input type='text'></span><span><input type='button' value='删除'></span>"; <span> </span>x.appendChild(li); } function deleteDepartment(obj) { <span> </span>if(window.confirm("确认删除吗?")){ <span> </span>var li = obj.parentNode.parentNode; //获得当前行 <span> </span>var ul = li.parentNode;//获得上一级标签 <span> </span>ul.removeChild(li); //删除当前行 <span> </span>} else { <span> </span>return false; <span> </span>} }

HTML:

<ul> <span> </span><li> <span> </span><span>所属院系:</span> <span> </span><span><input type="button" value="增加"></span> <span> </span></li> <span> </span><li> <span> </span><span> <span> </span><select> <span> </span><c:if test="${baseInstitutes !=null}"> <span> </span><c:forEach items="${baseInstitutes}" var="baseInstitute"> <span> </span><option value="${baseInstitute.id}">${baseInstitute.instituteName}</option> <span> </span></c:forEach> <span> </span></c:if> <span> </span>/select> <span> </span></span> <span> </span><span>专业:</span> <span> </span><span><input type="text"></span> <span> </span></li></ul>

以上就是小编为大家带来的JS实现动态增加和删除li标签行的实例代码全部内容了,希望大家多多支持脚本之家~

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzdxsw.html