我想在Prototype中的现有事件处理程序之前添加一个事件监听器/处理程序。这是一个例子:
<form ... onsubmit="alert('foo')" id="f1">
$('f1').observe("submit", function() {
alert('do this before foo');
});
</form>
答案 0 :(得分:0)
Observer = Class.create({
initialize: function() {
this.callbacks = [];
},
prependCallback = function(callback) {
this.callbacks.unshift(callback);
},
appendCallback = function(callback) {
this.callbacks.push(callback);
},
runCallbacks = function() {
this.callbacks.each(
function(callback) {
callback();
}
);
}
});
bunchOfCallbacks = new Observer();
bunchOfCallbacks.appendCallback(function(){alert('second');});
$('anything').observe('anyEvent', bunchOfCallbacks.runCallbacks);
bunchOfCallbacks.prependCallback(function(){alert('first');});