Reusing code between different event handlers

前端 未结 3 461
臣服心动
臣服心动 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:40

    define a function and do the job;

    var funCalled = function(){
        //your detailed actions
    }
    

    and call it in different conditions!

    if (some condition) {
        $(document).on('click','.selected-option',function(event){
            funCalled()
        })
    } else {
        $('.selected-option').click(function(event){
            funCalled()
        });
    }
    

提交回复
热议问题