gpt4 book ai didi

javascript - 触摸事件在 Android PhoneGap WebView(甚至内置浏览器)中不起作用

转载 作者:行者123 更新时间:2023-11-28 10:59:00 25 4
gpt4 key购买 nike

我使用的是phonegap,需要使用“touchstart”来加速点击响应,结果发现“touchstart”、“touchmove”、“touchend”都没有触发,而是“click”触发。

我什至在内置浏览器(7英寸android 4.0.3平板电脑)中测试了一个简单的页面,发现它失败了。

我在两台平板电脑上测试过还是一样。在下面的页面中,button2仅显示“html_click”,button3仅显示“click”:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf8">
<meta name="viewport" content="target-densitydpi=device-dpi, width=device-width">
<title>Droid touch event test.</title>
</head>
<body>
<button id="button">touch it</button>
<button id="button2" ontouchstart="d_('html_touchstart')" onclick="d_('html_click')">html</button>
<button id="button3">touch 3</button>
<div id="db"></div>
<script>
function $(d){return document.getElementById(d)}
function d_(m){dbo.innerHTML=dbo.innerHTML+' '+m}
function ts1(e){d_('document touched')}
function ts2(e){d_('button touched')}
function eh(e){
d_(e.type);
}
var ets='touchstart',dbo=$('db');
document.addEventListener(ets,ts1,false);
$('button').addEventListener(ets,ts2,false);
$('button3').addEventListener(ets,eh,false);
$('button3').addEventListener('touchend',eh,false);
$('button3').addEventListener('click',eh,false);
</script>
</body>
</html>

为什么“touchstart 没有触发?”我应该做些什么来使触摸事件起作用吗?或者有其他方法可以使点击响应更快吗?

我使用了 mousedown 事件,有效,但速度不是特别快。

(new Date()).getTime() mousedownclick 之间的差异(包括一些 innerHTML 更改) code>只有15左右。

最佳答案

仅保留向您展示概念所需的内容,我编写了此内容并在 ICS 设备上使用 Phonegap Build(默认为 Phonegap 2.0,但不重要)进行了测试。应该适合你:

<!DOCTYPE html>
<html>
<body>
<button id="button1">1</button>
<button id="button2">2</button>

<script>
document.getElementById("button1").addEventListener('touchstart',button1Pressed);
document.getElementById("button2").addEventListener('touchstart',button2Pressed);
function button1Pressed ()
{
alert('Button 1 Pressed');
}

function button2Pressed ()
{
alert('Button 2 Pressed');
}
</script>
</body>
</html>

关于javascript - 触摸事件在 Android PhoneGap WebView(甚至内置浏览器)中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12453230/

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