How to remove $(document).on click event?

前端 未结 7 2042
孤独总比滥情好
孤独总比滥情好 2021-01-06 08:13

Here is the code to add event

   $(document).on({
      click: function() {
        $(this).hide();
        $(\'#form_name\').removeClass(\'hide\');
                 


        
7条回答
  •  無奈伤痛
    2021-01-06 08:45

    You need to pass the event to unbind to .off(), also see the use of namepsaced event names

    $(document).on({
        'click.myevent': function () {
            $(this).hide();
            $('#form_name').removeClass('hide');
            $('#form_template_name')
                .attr('placeholder', $(this).text())
                .focus();
        }
    }, '.form-template-name');
    

    and

    $(document).off('click.myevent', '.form-template-name');
    

    Demo: Fiddle

提交回复
热议问题