2016-05-16 10 views
1

javascriptのモバイルタッチイベントの構文は何ですか?私が試した:純粋なjavascriptでモバイルタッチイベントを使用するにはどうすればよいですか?

window.document.body.ontouchstart = function() { alert(); }

window.document.body.touchstart = function() { alert(); }

をそれはすべてのエラーを与えるものではありません。ウェブページに触れても何も起こりません。それはaddEventListenerのように思えます。しかし、なぜwindow.document.body.ontouchstartは直接動作しないのですか?

答えて

2
var theElement = document.getElementById("theElement"); 

theElement.addEventListener("touchstart", handlerFunction, false); 

function handlerFunction(event) { 
alert(); 
} 
+1

を? – user31782

+0

@ user31782 'addEventListener()'メソッドが必要なので、body要素をターゲットにしてイベントをアタッチしてください。 'document.getElementsByTagName(" body ")[0] .addEventListener( 'touchstart' ....' –

1

このコード試してみてください: `動作しwindow.document.body.ontouchstart`しないのはなぜ

function foo(event) { 
    alert(); 
} 

var el = document.getElementsByTagName("canvas")[0]; 
    el.addEventListener("touchstart", foo(), false); 

//or 

window.document.body.addEventListener("touchstart", foo(), false); 
関連する問題