6
私はphonegapを使用してAndroidアプリを構築しています。JSを使用してphonegapでユーザータッチを検出する方法
ユーザーからのタッチイベントを検出したいので、アラートをポップアップ表示できます。しかし、どのように私はJavaScriptからontouchイベントを呼び出すのですか?
ありがとうございます!
私はphonegapを使用してAndroidアプリを構築しています。JSを使用してphonegapでユーザータッチを検出する方法
ユーザーからのタッチイベントを検出したいので、アラートをポップアップ表示できます。しかし、どのように私はJavaScriptからontouchイベントを呼び出すのですか?
ありがとうございます!
以下は、touchstart
とtouchend
を示す例です。タッチイベントをアタッチするには、要素属性またはJavaScriptのaddEventListener
の2つの方法があります。
タッチイベントをリスニングしているため、イベントはデスクトップのブラウザ(マウスイベントをサポートしています)で発生しません。このページをテストするには、AndroidまたはiOSシミュレータを開きます。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0;" />
<style type="text/css">
a {
color:black;
display:block;
margin:10px 0px;
}
</style>
<script type="text/javascript">
function onload() {
document.getElementById('touchstart').addEventListener('touchstart', hello, false);
document.getElementById('touchend').addEventListener('touchend', bye, false);
}
function hello() {
alert('hello');
}
function bye() {
alert('bye');
}
</script>
<title>Touch Example</title>
</head>
<body onload="onload();">
<h1>Touch</h1>
<a href="#" ontouchstart="hello();return false;">Attribute: ontouchstart</a>
<a href="#" ontouchend="bye();return false;">Attribute: ontouchend</a>
<a href="#" id="touchstart">addEventListener: touchstart</a>
<a href="#" id="touchend">addEventListener: touchend</a>
</body>
</html>
私は試してみます。ありがとう – Dayzza
@mwbrroks試してみましたが、リンク/ボタンでうまく動作します。しかし、私はそれが何もリンクやボタンなしで空白の画面上で動作することができますかどうか疑問に思っていた..長いユーザーが画面上の任意の場所にポップアップを表示するタッチ。出来ますか? :) thanks – Dayzza
window.addEventListener(etc)を実行してください – Harry