Estoy tratando de lograr lo que puede hacer la función jQuery live(), pero en JavaScript. ¿Alguien puede ayudar con esto?jQuery live() en JavaScript sin formato?
Gracias!
Estoy tratando de lograr lo que puede hacer la función jQuery live(), pero en JavaScript. ¿Alguien puede ayudar con esto?jQuery live() en JavaScript sin formato?
Gracias!
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.
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]
}
}
}
Ha intentado leer el código fuente de jQuery? –