Insert a JQuery click handler that executes before the ones already registered

前端 未结 1 1263

I want to attach a click event handler to each and every (Button, Anchor, Select) on all pages in my application, and that handler should run before the already attached ha

相关标签:
1条回答
  • 2020-12-17 04:41

    This should rearrange your click handlers.

    $(document).ready(function() {
        $("input[type=button]").each(function() {
    
            // your button
            var btn = $(this);
    
            var clickhandler = this.onclick;
            this.onclick = null;
    
    
            // new click handler
            btn.click(function() {
                alert('Prepended Handler');
            });
            btn.click(clickhandler);
        });
    });
    
    function f2() {
    alert('Handler declared in HTML');
    }
    
    0 讨论(0)
提交回复
热议问题