码迷,mamicode.com
首页 > Web开发 > 详细

js事件委托

时间:2015-07-07 12:47:49      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html style="width: 100%;height: 100%;">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0">
<meta charset="utf-8">
<head>
<style>
*{
padding: 0;
margin: 0;
border:0;
}
</style>
<script type="text/javascript" src="zepto.min.js"></script>
</head>
<body style="width:100%;height:100%;">
<ul id="ul">
<li>aaaaaaaa</li>
<li>bbbbbbbb</li>
<li>cccccccc</li>
</ul>
<button id="btn" style="width:100px;height:100px;"></button>
<button id="btn1" style="width:100px;height:100px;"></button>
</body>
<script>
window.onload = function(){
var oUl = document.getElementById("ul");
var aLi = oUl.getElementsByTagName("li");
var oBtn = document.getElementById("btn");
var iNow = 4;

oUl.onclick = function(ev){
var ev = ev || window.event;
var target = ev.target || ev.srcElement;
if(target.nodeName.toLowerCase() == "li"){
target.style.background = "red";
}
}
oBtn.onclick = function(){
iNow ++;
var oLi = document.createElement("li");
oLi.innerHTML = 1111 *iNow;
oUl.appendChild(oLi);
}
}
</script>
</html>

js事件委托

标签:

原文地址:http://www.cnblogs.com/timeAnouk/p/4626413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!