jQuery: difference between .click() AND .on(“click”)

后端 未结 3 1398
旧时难觅i
旧时难觅i 2020-11-27 06:20

I usually use

$(selector).click(...

But some people recommend me to use this instead:

$(selector).on(\"click\", function(.         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 06:35

    In the plain .click(... if the target of the selector changes on the fly (e.g via some ajax response) then you'd need to assign the behavior again.

    On .live(... the behavior will be automatically be reapplied on the selector.

    The .on(... is very new (jQuery 1.7) and it can cover the live scenario using delegated events which is a faster way to attach behavior anyway.

提交回复
热议问题