gpt4 book ai didi

javascript - touchend 事件被触发两次

转载 作者:行者123 更新时间:2023-12-03 02:48:31 26 4
gpt4 key购买 nike

这是我的代码,以防 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();
});

关于javascript - touchend 事件被触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47993515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com