Javascript 点击Phonegap中的事件
我发现一个问题,在我最近的项目中,当我“点击”或“单击”某个元素时,它将被触发两次。例如:Javascript 点击Phonegap中的事件,javascript,android,jquery,cordova,hybrid-mobile-app,Javascript,Android,Jquery,Cordova,Hybrid Mobile App,我发现一个问题,在我最近的项目中,当我“点击”或“单击”某个元素时,它将被触发两次。例如: ...... <div id='target'></div> ...... <script> $('#target').click(function(){alert('touched!')}); </script> 。。。。。。 ...... $(“#目标”)。单击(函数(){alert('toucted!'}); 然后它会提醒“触摸
......
<div id='target'></div>
......
<script>
$('#target').click(function(){alert('touched!')});
</script>
。。。。。。
......
$(“#目标”)。单击(函数(){alert('toucted!'});
然后它会提醒“触摸”两次
我想如果我使用手势库,我可以修复它,所以我尝试了Hammer.js。但问题似乎仍然无法修复。我对此感到困惑,真的希望有人能给我一些建议。谢谢 试试这个
<script>
$(document).ready(function(){
$('#target').click(function(){alert('touched!')});
});
</script>
$(文档).ready(函数(){
$(“#目标”)。单击(函数(){alert('toucted!'});
});
seee
试试这个
$(document).ready(function(){
$('#target').unbind('click');
$('#target').bind('click',function(event){
alert('touched!')
});
});
有时,事件触发会发生多次,因为它被绑定了多次。在使用phonegap进行项目时,我遇到了一个类似的问题(我认为问题主要出现在android jelly bean设备上)。我就是这么做的:
var isEvent = false;
$(document).ready(function(){
$(element).click(function(){
if(!isEvent){
isEvent = true;
doSomething();
}
setTimeout(function(){isEvent=false;},50); // time in ms should be set according to your needs
});
});
当时,这是唯一一个100%有效的解决方案。尝试以下方法:
$(document).on('pageinit', 'page_id', function(){
$('#target').click(function(){alert('touched!')});
});
在现代浏览器中,您只需使用touchstart和touchend即可
document.addEventListener('touchstart', function(){
alert('touch started at ' + Date.now() );
}, false);
document.addEventListener('touchend', function(){
alert( 'touch ended at ' + Date.now() );
});
通过设置触摸设备,您可以在chrome开发者工具中测试触摸事件。不知道其他浏览器是否也支持此功能我在我的android上尝试了你的代码,但再次失败!!!!我真的不知道怎么修。。。。。