Reusing code between different event handlers

前端 未结 3 424
臣服心动
臣服心动 2021-01-26 12:06

I have one code block which I want to invoke in different scenarios when a click is triggered, depending on whether the event is direct or is delegated.

But on changing

3条回答
  •  盖世英雄少女心
    2021-01-26 12:48

    var testfunction = function(currentObj){
      // your code here
    }   
    
     if (some condition)
      {
          $(document).on('click','.selected-option',function(event){
              testfunction($(this));  
          });
      }
     else {
          $('.selected-option').click(function(event){
              testfunction($(this));  
           });
      }
    

提交回复
热议问题