与遗留代码交互,我得到了这样的东西:
Event.observe(some_form, 'submit', [some anonymous function])
我想重新获取匿名事件处理程序,在 Prototype 中是否有一种简单的方法可以做到这一点?
最佳答案
这取决于 Prototype 的版本。来自更一般的答案I wrote previously :
版本 1.5.x:
// inspect Event.observers.each(function(item) { if(item[0] == some_form && item[1] == 'submit') { alert(item[2]) // [some anonymous function] } })
版本 1.6 到 1.6.0.3(含)(这里非常困难)
// inspect. "_eventId" is for < 1.6.0.3 while // "_prototypeEventID" was introduced in 1.6.0.3 var submitEvents = Event.cache[some_form._eventId || (some_form._prototypeEventID || [])[0]].submit; submitEvents.each(function(wrapper){ alert(wrapper.handler) // [some anonymous function] })
[当前]版本 1.6.1(稍微好一点)
// inspect var submitEvents = some_form.getStorage().get('prototype_event_registry').get('submit'); submitEvents.each(function(wrapper){ alert(wrapper.handler) // [some anonymous function] })
关于javascript - 有没有办法取回在 Prototype 中使用 Event.observe 注册的匿名事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1655943/