Duplicate one element's event handlers onto another?

前端 未结 6 1536
半阙折子戏
半阙折子戏 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:28

    Is this what you are looking for?

    var clickHandler = function() { alert("Handled!"); }
    $('#firstEl').click(clickHandler);
    $('#secondEl').click(clickHandler);
    

提交回复
热议问题