jQuery live()在纯JavaScript中?

我试图完成jQuery live()函数可以做的事情,但是在纯JavaScript中。 这里有人可以帮忙吗?

谢谢!

这是一个小小的启动示例

document.onclick = function(evt){ evt = evt || window.event; var element = evt.target || evt.srcElement; }; 

无论您在何处单击,都会获得对接收到该单击的元素的引用。

但是,在实际场景中更有用的是对IE使用attachEvent方法,或者对其余方法使用addEventListener

像这样的东西:

 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] } } }