Rebind DOM Event with jQuery
This is purely a theoretical question so I'm not looking for alternative solutions.
Is there some way of getting the default handler to do something like this
var default开发者_运维知识库Handler = $("#test").click;
$('#test').unbind('click');
$('#test').bind('click', defaultHandler);
You can access the .data('events')
object, which is used to store all event handler information:
$(document).ready(function() {
var $test = $('#test');
$test.bind('click', function() {
alert('default handler');
});
var storedClick = $test.data('events').click[0].handler;
$test.unbind('click');
$('#restore').click(function() {
$test.bind('click', storedClick);
});
});
See this in action: http://www.jsfiddle.net/76GPF/
Remember, the events object
is holding Arrays
, so in the real world you should store the complete array information. I just stored the very first handler in this example.
精彩评论