javascript - Ask a small question about the touchstart event
仅有的幸福
仅有的幸福 2017-06-08 11:03:12
0
2
831

There is a piece of code that I am confused about

var clickEventType=((document.ontouchstart!==null)?'click':'touchstart');

If it says document.ontouchstart!==null, it means that touchstart exists, and it should look like this:

var clickEventType=((document.ontouchstart!==null)?'touchstart':'click')

Why is it written the way above?

仅有的幸福
仅有的幸福

reply all(2)
巴扎黑

If written as

var clickEventType=((typeof document.ontouchstart ==="undefined")?'click':'touchstart')

You will understand, ontoucstart is an attribute of the doucment object, but ontouchstart points to a reference to the function, that is, ontouchstart points to an object. When it is not pointed to, ontouchstart needs an initial value. This initial value is null. If the touchstart event is supported, then the initial value of ontouchstart is set to null, so it is written like this

typecho
document.ontouchstart == null;

This shows that the current browser supports ontouchstart, because if it does not support it, the value of document.ontouchstart is undefined
, and if document.ontouchstart is not bound to the event, it is equal to null, so you You can use it like thisdocument.ontouchstart = function(){};

The initial value is Null. If it is not equal to null, it can be said that it is not supported

document.ontouchstart !== null;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!