【html】动态增加横向带删除的列表

需要实现的效果

这里写图片描述

代码

html

<ul id="projUl">
                <label style="float: left; " class="l-btn-text">&nbsp;项目名称列表</label>
            </ul>

js代码

    function addLi(projName,projId){
        var li_1=document.createElement("li");
        addSpan(li_1,projName,projId);
        addHiddenSpan(li_1,projId);
        addDelBtn(li_1,projId);
        document.getElementById("projUl").appendChild(li_1);
    }
    //为姓名或邮箱等添加span标签,好设置样式
    function addSpan(li,text,id){
        var span_1=document.createElement("input");
        span_1.setAttribute("id",id);
        span_1.setAttribute("name","projectNames");
        span_1.setAttribute("class","form-control");
        span_1.setAttribute("style","width: 180px;height:24px");
        span_1.setAttribute("type","text");
        span_1.setAttribute("value",text);
        li.appendChild(span_1);
    }
    function addHiddenSpan(li,text){
        var span_1=document.createElement("input");
        span_1.setAttribute("name","projectIds");
        span_1.setAttribute("class","form-control");
        span_1.setAttribute("type","hidden");
        span_1.setAttribute("style","width: 180px;height:24px");
        span_1.setAttribute("value",text);
        li.appendChild(span_1);
    }
    //添加删除按钮及设置删除按钮的样式及添加点击事件
    function addDelBtn(li,id){
        var btn=document.createElement("a");
        btn.setAttribute("name",id);
        btn.setAttribute("onclick","delBtnData(this)");
        btn.setAttribute("href","javascript:void(0);");
        btn.setAttribute("class","easyui-linkbutton ebsButton detailButtonc l-btn l-btn-small l-btn-plain");
        btn.setAttribute("data-options","iconCls:'fa fa-1x fa-fw fa-trash',plain:true");
        var span_1=document.createElement("span");
        span_1.setAttribute("class","l-btn-left l-btn-icon-left");
        var span_2=document.createElement("span");
        span_2.setAttribute("class","l-btn-text l-btn-empty");
        span_2.innerHTML='&nbsp;'
        var span_3=document.createElement("span");
        span_3.setAttribute("class","l-btn-icon fa fa-1x fa-fw fa-trash");
        span_1.appendChild(span_2);
        span_1.appendChild(span_3);

        btn.appendChild(span_1);
        li.appendChild(btn);
    }
    //为删除按钮添加删除节点功能
    function delBtnData(obj){
        var ul=document.getElementById("projUl");
        var oLi=obj.parentNode;
        //obj.parentNode指删除按钮的span层
        //obj.parentNode.parentNode为li层
        ul.removeChild(oLi);
    }

调用例子

addLi("哈哈哈哈",1234);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值