<!DOCTYPE html><html><head><metacharset="utf-8"/><title></title></head><body><ul><li>1</li><li>2</li><li>3</li><li>4</li></ul><scripttype="text/javascript">// 非代理(遍历子节点)functionclickLi(){var li = document.querySelectorAll('li');for(var i =0; i < li.length; i++){
li[i].addEventListener('click',(function(i){returnfunction(){
console.log('非代理:','index:', i,', txt:',this.textContent,';');}})(i),false);}}clickLi();</script></body></html>
2. 事件代理【利用父节点接收冒泡】
<!DOCTYPE html><html><head><metacharset="utf-8"/><title></title></head><body><ul><li>1</li><li>2</li><li>3</li><li>4</li></ul><scripttype="text/javascript">// 代理(父节点接收)functionclickUl(){var ul = document.querySelector('ul');
ul.addEventListener('click',function(e){var e = e || window.event;var tag = e.target || e.srcElement;if(tag.tagName.toLowerCase()==='li'){// var index = [].indexOf.call(document.querySelectorAll('li'), tag);var list =[].slice.call(document.querySelectorAll('li'));var index = list.indexOf(tag);
console.log('代 理:','index:', index,', txt:', tag.textContent,';');}});}clickUl();</script></body></html>