JavaScript动态添加与删除列表项(li)的完整实现
作者:窗边听雨时间:2026-10-30 15:21:44浏览:536
本文提供使用JavaScript动态增加和删除ul中li标签行的完整代码示例。通过获取父元素节点长度,利用createElement创建新li并设置类名与内部HTML结构,实现动态添加功能;同时通过removeChild移除指定节点实现删除功能,适用于表单行动态管理场景。
JavaScript动态增删列表项实现
具体实现方式如下所示:
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'>专业:</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; //获得当前行
<span style="white-space:pre"> </span>var ul = li.parentNode;//获得上一级标签
<span style="white-space:pre"> </span>ul.removeChild(li); //删除当前行
<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">专业:</span>
<span style="white-space:pre"> </span><span class="nr6"><input name="specialty" type="text"></span>
<span style="white-space:pre"> </span></li></ul>
以上就是小编为大家带来的JS实现动态增加和删除li标签行的实例代码全部内容了,希望大家多多支持本站~
作者最新文章
anime.js 快速入门:怎么引入库并编写第一个 DOM 动画
2026-11-05 14:22:43
在线JS解密后变量名仍乱码?结合浏览器DevTools调试还原
2026-11-04 20:26:01
怎么在浏览器中运行JavaScript代码并查看输出结果
2026-11-04 09:07:33
JS 获取屏幕分辨率:window.screen.width 与 height 用法
2026-11-04 08:52:48
为什么推荐用DOMContentLoaded代替window.onload初始化JS
2026-11-04 08:42:33
