touchend事件被触发两次

touchend事件被触发两次

问题描述:

这是我的情况下,代码touchend事件发生:touchend事件被触发两次

$('body').on('click touchend', '.typeSelect', function(){ 
    var Classes = $(this).attr('class').split(" "); 
    var width1 = $(this).width(); 
    $('.active').removeClass('active'); 
    $(this).addClass('active'); 
    $('.typeDropDownList').hide(); 
    $('.'+Classes[0]+'List').css({'width' : width1+12}).toggle(); 
}); 

如果事件是click,一切工作正常,但如果它的touchend,这个函数被调用两次。这是为什么?

+0

会发生什么,当你做'touchend'只有 - '$( '身体')上( 'touchend',' – gurvinder372

+1

移动浏览器会同时触发'点击'和'touchend' –

+1

[触发事件触发两次]的可能重复(https://stackoverflow.com/questions/25165360/touch-events-firing-twice) – RogerC

关闭点击,如果事件类型touchend

$('body').on('click touchend', '.typeSelect', function(e){ 
    e.stopPropagation(); 
    e.preventDefault(); 
     if(e.type == 'touchend'){ 
      $(this).off('click'); 
     } 
    var Classes = $(this).attr('class').split(" "); 
     var width1 = $(this).width(); 
     $('.active').removeClass('active'); 
     $(this).addClass('active'); 
     $('.typeDropDownList').hide(); 
     $('.'+Classes[0]+'List').css({'width' : width1+12}).toggle(); 
    }); 
+0

不起作用。在发布我的问题之前,我尝试了这种方法。 –

+0

@VaxoBasilidze尝试更新代码 – Ghostman

+0

确实有效,谢谢! –