The following jquery code, why immediately after off. On?

function businessEvent(){
      var that = this;
    $(".business .business_item").each(function() {
        $(this).hover(function() {
            $(this).addClass("active");
        }, function() {
            $(this).removeClass("active");

        })
    }).off().on("click", function() {
        var businessType = $(this).attr("data-index");
        var name = $(this).attr("data-name");
        $(".business").attr("style","display:none");
        $(".business_navigation").attr("style","display:block");
        $.ajax({
            type : "post",
            url : "../hf/client/setBusinessType",
            dataType : "json",
            timeout:60000,
            async : false,
            data : {
                businessType:businessType,
                customerSessionId:sessionStorage.customerSessionId
            },
            success : function(resp) {
                
            }
        });
    });
  }
Jul.12,2021

in this way, businessEvent can be called multiple times without having to worry about adding an additional click event for each call.


add events to the clicked dom after unbinding all the events, which are generally used in the loop to avoid binding redundant events and causing multiple execution.

Menu