touchend事件被触发两次(touchend event gets triggered twice)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
touchend事件被触发两次(touchend event gets triggered twice)

这是我的代码,以防发生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 ,这个函数被调用两次。 这是为什么?

This is my code in case touchend event occurs:

$('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(); });

If the event is click, everything works fine, but if it's touchend, this function gets called twice. Why is that?

最满意答案

如果事件类型被触发,请关闭点击

$('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(); });

Turn off click if event type is 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(); });

更多推荐

本文发布于:2023-04-20 16:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/45ad4bf3645900ec396c9f26ed6bdbd5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   事件   touchend   event   triggered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!