e.PreventDefault()& e.stopPropogation()不适用于平板电脑和移动 [英] e.PreventDefault() & e.stopPropogation() not working on tablet & mobile
问题描述
我在js文件中有以下代码,该文件适用于所有桌面浏览器,但不适用于以下平板电脑和手机
I have below code in a js file which works fine on all desktop browsers but not on the following tablet & phone
- 三星S4(镀铬和默认浏览器)
- iPhone 4S(Chrome& safari) )
在ipad air / mini上(使用chrome& safari)它可以正常工作
在HTC 1上(使用chrome& ;默认)它工作正常
On ipad air/mini (using chrome & safari) it works fine On HTC 1 (using chrome & default) it works fine
有没有人对此有任何想法? - 似乎非常多的设备间歇性令人沮丧
Does anyone have any ideas on this? - it seems very much device intermittent which is frustrating
var clicked = false;
$('header > div > nav > ul > li').first().find('a').on("click", function (e) {
if (clicked === false) {
clicked = true;
return true;
} else {
e.preventDefault();
e.stopPropagation();
return false;
}
});
代码基本上是在第一次点击后禁用锚链接点击事件(当用户点击时页面重新加载它 - 在href中设置)并在浏览器再次呈现页面时重新启用。
The code is basically disabling an anchor link click event after the first click (the page reloads when user clicks it - set in href) and gets re-enabled when browser renders page again.
代码已经发展到你上面看到的......我最初只有 e.preventDefault();
in在我点击移动设备之前,在桌面测试中工作正常的else块。
The code has grown to what you see above.. i originally just had e.preventDefault();
in the else block which worked fine on desktop testing until i hit the mobile devices.
推荐答案
移动浏览器没有点击事件。你需要使用touchstart或touchend。
Mobile browsers don't have a click event. You need to use touchstart or touchend.
$(...).on("click touchstart", function (e) {
应该做的诀窍
这篇关于e.PreventDefault()& e.stopPropogation()不适用于平板电脑和移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!