2010-12-09 14 views

Respuesta

2

Aquí hay un pequeño ejemplo de inicio

document.onclick = function(evt){ 

    evt = evt || window.event; 
    var element = evt.target || evt.srcElement; 

}; 

donde se haga clic se obtiene una referencia al elemento que recibió el clic.

Más útil, sin embargo, en un escenario real sería utilizar el método attachEvent para IE, o el addEventListener para el resto.

2

Algo como esto:

myLive("div", "click", function() { ... }); 

var liveArray = []; 

function myLive(selector, type, handler) { 
    liveArray.push([selector, type, handler]); 
} 

// this handler should fire for any event on the page, and should be attached 
// to the document node 
function documentAnyEvent(e) { 
    var e = e || window.event; 
    var target = e.target || e.srcElement; 
    for (var i = 0; i < liveArray.length; i++) { 
     if (target mathes the selector AND e.type matches the type) { 
      // fire the handler liveArray[i][2] 
     } 
    } 
}