日期:2014-05-16 浏览次数:20347 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<TITLE> New Document </TITLE>
<script type="text/javascript">
window.onload = function() {
document.getElementById('ul').onclick = function(e) {
e = e
------解决方案--------------------
window.event;
var target = e.target
------解决方案--------------------
e.srcElement;
// 不是<li>标签就返回
if(target.nodeName !== 'LI') {
return;
}
//
func(target.innerHTML);
// 阻止默认行为并取消冒泡
// 阻止打开连接
if(typeof e.preventDefault === 'function') {
e.preventDefault();
e.stopPropagation();
}else {
e.returnValue = false;
e.cancelBubble = true;
}
}
}
var i=0;
function addLI(){
var li = document.createElement('li');
li.innerHTML = 'new Li ' + (i++);
document.getElementById('ul').appendChild(li);
}
function func(s) {
alert(s)
}
</script>
</HEAD>
<BODY>
<ul id="ul">
<li>百度</li>
<li>谷歌</li>
</ul>
<INPUT TYPE="button" VALUE="添加LI" ONCLICK="addLI()">
</BODY>
</HTML>
function action(ev){
console.log(this.id);
}
window.onload = function(){
var ul = document.getElementById('ul');
var lis = ul.getElementsByTagName('li');
var len = lis.length;
for(var i=0;i<len;i++){
lis[i].onclick = function(ev){
ev = ev
------解决方案--------------------
window.event;
action.call(this);
}