Using Jquery off().on() or just on()

前端 未结 3 420
囚心锁ツ
囚心锁ツ 2020-12-14 06:28

I know that we can attach events with the jQuery on() function and remove them again with off().

In an existing project I do sometimes come

3条回答
  •  生来不讨喜
    2020-12-14 07:05

    Actually you can attach multiple event handlers to the same event, not just one. Even when namespaces are used. Check out this quick demo.

    Actually, one advantage of namespaces is that you can mark a group of event handlers, and easily remove them later to avoid attaching them several times. This is what this line does:

    $("#SomeId").off("click.namespace").on("click.namespace"); 
    

提交回复
热议问题