zoukankan      html  css  js  c++  java
  • js事件委托,可以使新添加的元素具有事件(event运用)

    miaov视频教程  http://study.163.com/course/courseMain.htm?courseId=231002

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <ul id="ul1">
      <li>1111</li>
      <li>2222</li>
      <li>3333</li>
      <li>4444</li>
      <li>5555</li>
    </ul>
    <input type="button" id="add" value="添加"/>
    <script>
    window.onload=function(){
    var oUl=document.getElementById('ul1');  //ul
    var oLi=oUl.getElementsByTagName('li');  //li
    var iNow=4;
    for(i=0;i<oLi.length;i++){
           this.onclick=function(ev){       //这里不用this,不能发生冒泡
           ev=ev || window.event;            //event兼容
           target=ev.srcElement || ev.target;  //event.target兼容
           if(target.nodeName.toLowerCase() == "li"){    //判断nodeName
               target.style.backgroundColor='red';   //背景红色
           }
       }
    }
    var oAdd=document.getElementById('add'); //点击添加按钮
      oAdd.onclick=function(){
        iNow++;
        var aLi=document.createElement('li');   //创建li节点
        aLi.innerHTML=iNow*1111;
        oUl.appendChild(aLi);                  //添加li节点
      }
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    tensorflow之tf.meshgrid()
    Python中数据的保存和读取
    透视投影推导
    tensorflow之tf.slice()
    tensorflow的tf.train.Saver()模型保存与恢复
    偶数分割求平均值
    母牛的故事
    统计一行的不重复的单词字符个数
    N个顶点构成多边形的面积
    贪心法基本入门
  • 原文地址:https://www.cnblogs.com/txxt/p/5782885.html
Copyright © 2011-2022 走看看