开发者

Rebind DOM Event with jQuery

开发者 https://www.devze.com 2023-02-05 14:33 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号