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
,这个函数被调用两次。这是为什么?
答
关闭点击,如果事件类型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();
});
会发生什么,当你做'touchend'只有 - '$( '身体')上( 'touchend',' – gurvinder372
移动浏览器会同时触发'点击'和'touchend' –
[触发事件触发两次]的可能重复(https://stackoverflow.com/questions/25165360/touch-events-firing-twice) – RogerC