How to change onClick handler dynamically?

前端 未结 11 1564
清歌不尽
清歌不尽 2020-12-02 15:37

I\'m sure there are a million posts about this out there, but surprisingly I\'m having trouble finding something.

I have a simple script where I want to set the onC

11条回答
  •  Happy的楠姐
    2020-12-02 16:11

    I think you want to use jQuery's .bind and .unBind methods. In my testing, changing the click event using .click and .onclick actually called the newly assigned event, resulting in a never-ending loop.

    For example, if the events you are toggling between are hide() and unHide(), and clicking one switches the click event to the other, you would end up in a continuous loop. A better way would be to do this:

    $(element).unbind().bind( 'click' , function(){ alert('!') } ); 
    

提交回复
热议问题