ul、ol列表的增删改实现,对比js和jquery

在页面中放置一个ul或ol列表,实现对列表中li标签的高亮选中、添加到最后(append),添加到指定位置之前(before)、之后(after),删除选中的li标签,修改选中的li标签。

html部分不变:

    <style type="text/css">
        ol li {
            border: 1px solid #0094ff;
            font-family: 微软雅黑;
            font-size: 30px;
            width: 200px;
        }
    </style>
    <body>
        <input type="button" value="添加标签到最后" onclick="AddToEnd()">
        <input type="button" value="添加到选中标签之前" onclick="InsertToBefore()">
        <input type="button" value="添加到选中标签之后" onclick="InsertToAfter()">
        <input type="button" value="删除选中标签" onclick="Del()">
        <input type="button" value="修改选中标签" onclick="Edit()">
        <ol id="olList">
            <li>张三</li>
            <li>李四</li>
            <li>王五</li>
        </ol>
    </body>

js代码实现:

<script type="text/javascript">

    var ulDom = getDomByID("olList");
    var domLis = getDomsByTagName("li");
    var selectedLi = undefined;
    for (var i = 0; i < domLis.length; i++)
    {
        domLis[i].onclick = highLight;
    }

    function MakeLi(liContent)
    {
        var newLi = document.createElement("li");
        newLi.innerHTML = liContent;
        newLi.onclick = highLight;
        return newLi;
    }

    function highLight()
    {
        var domLis = getDomsByTagName("li");
        for (var i = 0; i < domLis.length; i++)
        {
            domLis[i].style.backgroundColor = "white";
        }
        this.style.backgroundColor = "yellow";
        selectedLi = this;
    }

    function getInput()
    {
        var inputStr = prompt("请输入标签内容", "在此输入");
        if (inputStr != undefined && inputStr != "在此输入")
        {
            return inputStr.trim();
        }
        else return "";
    }

    function AddToEnd()
    {
        var inputStr = getInput();
        if (inputStr != "")
        {
            var newLi = MakeLi(inputStr);
            ulDom.appendChild(newLi);
        }
    }

    function InsertToBefore()
    {
        var inputStr = getInput();
        if (inputStr != ""&&selectedLi!=undefined)
        {
            var newLi = MakeLi(inputStr);
            ulDom.insertBefore(newLi,selectedLi);
        }
    }

    function Edit()
    {
        if (selectedLi != undefined)
        {
            var inputStr = getInput();
            if (inputStr != "" && inputStr != selectedLi.innerHTML)
            {
                selectedLi.innerHTML = inputStr;
            }
        }
    }

    function Del()
    {
        if (selectedLi != undefined)
        {
            ulDom.removeChild(selectedLi);
            selectedLi = undefined;
        }
    }
</script>

jquery代码实现,在修改时变为直接在原来的li标签内生成文本框,可以直接在该位置修改,修改结束文本框消除,文本框值保留在li标签。

    <script src="JS/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        var $liSelObj=undefined;//纪录当前选中的li标签
        var $inputObj = $("<input type='text' />");//定义一个初始编辑文本框
        var isReadyEdit = true;//当前是否可以编辑

        //等同于$(document).ready(function () { })
        $(function () {
            //为所有li标签绑定点击高亮事件,要注意js里是onclick=方法名,jquery里是click(方法名),不要搞混
            $("li").click(selHighLight);
        })

        //li标签的点击高亮方法
        function selHighLight()
        {
            //还原所有li标签背景色
            $("li").css("background-color", "white");
            //设置当前li标签的背景色为黄色
            $(this).css("background-color", "yellow");
            //纪录当前选中的li标签
            $liSelObj = $(this);
        }

        //创建一个新的li标签
        function makeLi()
        {
            var inputStr = prompt("请输入标签内容", "这里输入");
            //判断是否输入
            if (inputStr != undefined && inputStr.trim() != "" && inputStr.trim() != "这里输入") {
                //创建一个li标签
                $liObj = $("<li>" + inputStr + "</li>");
                //为创建的li标签绑定点击高亮选中事件
                $liObj.click(selHighLight);
                return $liObj;
            }
            else return undefined;//用于判断是否创建了li标签
        }

        //创建一个li标签,添加到最后
        function AddToEnd()
        {
            $newLiObj = makeLi();
            if ($newLiObj != undefined)
            {
                $("#olList").append($newLiObj);
            }
        }

        //在选中li标签之前插入一个新li标签
        function InsertToBefore()
        {
            //判断当前是否有标签选中
            if ($liSelObj) {
                $newLiObj = makeLi();
                if ($newLiObj != undefined) {
                    $newLiObj.insertBefore($liSelObj);
                }
            }
            else alert("请先选中一个li标签");
        }

        //在选中li标签之后插入一个新li标签
        function InsertToAfter() {
            //判断当前是否有标签选中
            if ($liSelObj) {
                $newLiObj = makeLi();
                if ($newLiObj != undefined) {
                    $newLiObj.insertAfter($liSelObj);
                }
            }
            else alert("请先选中一个li标签");
        }

        //删除选中li标签
        function Del()
        {
            //判断当前是否有标签选中
            if ($liSelObj) {
                $liSelObj.remove();
                $liSelObj = undefined;
            }
            else alert("请先选中一个li标签");
        }

        //修改选中li标签
        function Edit()
        {
            //判断当前是否有标签选中
            if ($liSelObj) {

                //判断当前是否可以编辑
                if (isReadyEdit)
                {
                    //获取当前选中li标签的文本,放在input控件中
                    var strCurLiText = $liSelObj.text();
                    $inputObj.val(strCurLiText);
                    //将li标签内的文本清除,替换为input标签
                    $liSelObj.html($inputObj);
                    //为文本框添加失去焦点事件
                    $inputObj.blur(loseFocus);
                    //文本框获得焦点,要先获得焦点,才能触发失去焦点事件
                    $inputObj.focus();
                    //在编辑状态中,不能再新建文本框
                    isReadyEdit = false;
                }
            }
            else alert("请先选中一个li标签");
        }


        //编辑文本框失去焦点事件
        function loseFocus() {
            //获取自己的文本值
            var oldStr = $(this).val();
            //将文本值,设置到自己的父元素中
            $(this).parent().html(oldStr);
            //重置为可以编辑
            isReadyEdit = true;
        }

    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的JavaScript代码示例,用于实现购物车的增加、删除和修功能: ```html <!DOCTYPE html> <html> <head> <title>购物车示例</title> <style> button { margin-right: 10px; } </style> </head> <body> <h2>购物车</h2> <ul id="cart"></ul> <p> <label for="name">商品名:</label> <input type="text" id="name" required> <label for="price">价格:</label> <input type="number" id="price" required> <button onclick="addItem()">添加</button> </p> <p> <label for="index">要修的商品索引:</label> <input type="number" id="index" required> <label for="newPrice">新价格:</label> <input type="number" id="newPrice" required> <button onclick="updateItem()">修</button> </p> <p> <label for="index">要删除的商品索引:</label> <input type="number" id="index" required> <button onclick="removeItem()">删除</button> </p> <script> var cart = []; var cartList = document.getElementById('cart'); function renderCart() { cartList.innerHTML = ''; for (var i = 0; i < cart.length; i++) { var item = cart[i]; var listItem = document.createElement('li'); listItem.innerHTML = item.name + ' - ¥' + item.price.toFixed(2); cartList.appendChild(listItem); } } function addItem() { var nameInput = document.getElementById('name'); var priceInput = document.getElementById('price'); var name = nameInput.value.trim(); var price = parseFloat(priceInput.value.trim()); if (name.length === 0 || isNaN(price)) { alert('商品名和价格必须填写且价格必须为数字'); return; } var item = { name: name, price: price }; cart.push(item); renderCart(); nameInput.value = ''; priceInput.value = ''; } function updateItem() { var indexInput = document.getElementById('index'); var newPriceInput = document.getElementById('newPrice'); var index = parseInt(indexInput.value.trim()); var newPrice = parseFloat(newPriceInput.value.trim()); if (isNaN(index) || isNaN(newPrice) || index < 0 || index >= cart.length) { alert('要修的商品索引必须填写且必须为介于0和' + (cart.length - 1) + '之间的整数,新价格必须填写且必须为数字'); return; } cart[index].price = newPrice; renderCart(); indexInput.value = ''; newPriceInput.value = ''; } function removeItem() { var indexInput = document.getElementById('index'); var index = parseInt(indexInput.value.trim()); if (isNaN(index) || index < 0 || index >= cart.length) { alert('要删除的商品索引必须填写且必须为介于0和' + (cart.length - 1) + '之间的整数'); return; } cart.splice(index, 1); renderCart(); indexInput.value = ''; } </script> </body> </html> ``` 在这个示例中,我们创建了一个数组`cart`来存储购物车中的商品信息。使用`renderCart`函数来更新购物车列表,使用`addItem`函数向购物车中添加新商品,使用`updateItem`函数更新购物车中的商品价格,使用`removeItem`函数删除购物车中的商品。我们还使用了一些DOM操作来获取和更新输入框中的值,以及动态地更新购物车列表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值