2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ontouchxxxxxは存在しない

Last updated at Posted at 2018-02-18

ontouchstart, ontouchmove, ontouchendは存在しない。

console.log(onmousedown); // => null
console.log(ontouchstart); // => Uncaught Error: 'ontouchstart' is not defined

当然、代入してもタッチイベントは取れない。

// マウスで左クリックする時のイベント
addEventListener('mousedown', function() { console.log('new mouse'); });

// 古いやり方(非推奨だが、可能)
onmousedown = function() { console.log('old mouse'); };


// スマホなどでタップする時のイベント
addEventListener('touchstart', function() { console.log('new touch'); });

// 古いやり方も可能かと思いきや、不 可 能
ontouchstart = function() { console.log('old touch'); };

ちなみにこの例では一貫してグローバルオブジェクト(window)だったが、
documentでも個別のHTML要素でも同様に無効である。

たとえ書き捨て目的でもサボらずaddEventListenerを使おう。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?