鼠标移入的小bug

来源:7-2 编程练习

最怕日日煲靓汤

2018-09-17 16:57:56

<!DOCTYPE html>

<html>


<head>

    <title></title>

    <style type="text/css">

    html,

    body {

        margin: 0;

        padding: 0;

    }


    div:not(:nth-of-type(2)) {

        width: 500px;

        height: 100%;

        background-color: #00c4ff7a;

        margin: 0 auto;

    }

    /*ul默认有外边距*/


    ul {

        width: 300px;

        height: 100%;

        background-color: #f9c3e6d6;

        list-style: none;

        /*清除默认边距*/

        margin: 0;

        padding: 0;

        margin: 20px auto;

        margin-bottom: 30px;

    }


    li {

        width: 200px;

        height: 30px;

        line-height: 30px;

        margin: 30px auto;

        background-color: #cdffc0;

    }

    </style>

</head>


<body>

    <div id="box">

        <button id="btnAdd">添加元素</button>

        <button id="btnRemove">删除元素</button>

        <ul id="list">我是ul

            <li>我是li1</li>

            <li>我是li2</li>

            <li>我是li3</li>

        </ul>

    </div>

    <script type="text/javascript">

        (function(){

            var ul=document.getElementById('list');

            var btnAdd=document.getElementById('btnAdd');

            var btnRemove=document.getElementById('btnRemove');

            var liList=ul.querySelectorAll('li',ul);

            var len=liList.length; 

            //获取ul里面所有的li

            var getLi=function(){

                 liList=ul.querySelectorAll('li',ul);

                 len=liList.length;                      

            };

            //添加鼠标移入移除事件

            function mouseEven(){

                for(var i=0;i<len;i++){

                    liList[i].onmouseover=function(){

                        this.style.backgroundColor='red';

                    };


                    liList[i].onmouseout=function(){

                        this.style.backgroundColor='green';

                    };

                }

            };

            //添加按钮鼠标点击事件

            btnAdd.onclick=function(){

                getLi();

                var newLi=createLi(len+1);

                ul.appendChild(newLi);

                

                mouseEven();

            };

            //删除按钮鼠标点击事件

            btnRemove.onclick=function(){          

                deleteLi();      

            };

            //创建一个新的li节点

            function createLi(number){

                var newLi=document.createElement('li');

                var txt=document.createTextNode('我是li'+number);

                newLi.appendChild(txt);

                return newLi;

            };

            function deleteLi(){

                getLi();

               ul.removeChild(liList[liList.length-1]);


            }

                   





           

        })()

    </script>

</body>


</html>

当我点击添加元素的时候   新添加的那个li元素 没有鼠标移入移出的效果   请问要怎么改进这个代码


写回答

1回答

好帮手慕星星

2018-09-17

这道题是无论是添加li还是移除li,始终都是前三个改变颜色哦,参考:

http://img.mukewang.com/climg/5b9f7bcf0001e12208780782.jpg

自己测试下,祝学习愉快~~

0

0 学习 · 4826 问题

查看课程