事件委托

    
  • 1
  • 9999999999999
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
window.onload = function(){
    var ul = document.getElementById("list");
    ul.onclick = function(eve){
        var event = eve || window.event;
        var target = event.target || event.srcElement;
        while(target.nodeName.toLowerCase() != 'li'){
            if(target === ul){
                target = null;
                break;
            }
            target = target.parentNode;
        }

        if(target){
            alert(target.innerHTML);
        }
    };
};

你可能感兴趣的:(事件委托)