Duplicate one element's event handlers onto another?

前端 未结 6 1531
半阙折子戏
半阙折子戏 2021-02-14 01:51

How do you copy an event handler from one element to another? For example:

$(\'#firstEl\')
    .click(function() {
        alert(\"Handled!\");
         


        
6条回答
  •  北海茫月
    2021-02-14 02:23

    You might be interested in the triggerHandler method

    // here's where the magic happens 
    //$('#secondEl').click = $('#firstEl').click; // ????
    $('#secondEl').click(function() {
        $('#firstEl').triggerHandler('click');
    });
    

提交回复
热议问题