What is the difference between jquery on with / without selector parameter and jquery delegate?

前端 未结 4 1820
孤街浪徒
孤街浪徒 2021-01-18 06:38

I am using jquery 1.10. I want to know what the difference is between these three functions.

Which function is better and why?

What is the purpose of the del

4条回答
  •  别那么骄傲
    2021-01-18 07:39

    $(".dropdown-menu").on("click", ".show_opt_menu", function() {
    alert("hello");
    });
    

    this function attaches click event to .dropdown-menu executes when the target element is .show_opt_menu that means when you dynamically add another .show_opt_menu dont need to attach click function. the parent is responsible for the click action.

    $(".dropdown-menu .show_opt_menu").on("click", function() {
    alert("hello");
    });
    

    this function attaches the click event to .show_opt_menu so when you dynamically add new .show_opt_menu you need to attach event separately to that.

    $(".dropdown-menu").delegate(".show_opt_menu", "click", function() {
    alert("Delegate");
    });
    

    same purpose of on

提交回复
热议问题