美文网首页
如何实现使用事件绑定写一个按钮,按钮每按一次生成一个新的li,l

如何实现使用事件绑定写一个按钮,按钮每按一次生成一个新的li,l

作者: chung_shu | 来源:发表于2017-08-19 02:17 被阅读0次

    第一步:获取页面的ul,button

    第二步:动态创建li元素

    第三步:把li元素追加到ul中

    第四步:

    ```

    Title

    ul li{

    width:200px;

    height:40px;

    border:1px solid#000;

    }

    .ac{

    color: red;

    }

    button{

    width:30px;

    height:20px;

    line-height:20px;

    background-color:#ffd8d7;

    }

      +

      window.onload=function() {

      varul=document.getElementsByTagName('ul')[0];

      varbtn=document.getElementsByTagName('button')[0];

      varli=document.createElement('li');

      li.innerHTML='hello';

      ul.appendChild(li);

      ul.onclick=function(e) {

      varev=event||window.event;

      vartargt=ev.target||window.srcElement;

      if(targt.tagName.toLowerCase() =='li'){

      if(targt.getAttribute('class')=='ac'){

      targt.setAttribute('class',' ');

      }else{

      targt.setAttribute('class','ac');

      }

      }

      };

      btn.onclick=function() {

      varnewli=li.cloneNode(true);

      ul.appendChild(newli);

      }

      }

      ```

      相关文章

        网友评论

            本文标题:如何实现使用事件绑定写一个按钮,按钮每按一次生成一个新的li,l

            本文链接:https://www.haomeiwen.com/subject/llwzrxtx.html