search

Home  >  Q&A  >  body text

javascript - Ask a small question about the touchstart event

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?

仅有的幸福仅有的幸福2819 days ago944

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-06-08 11:05:12

    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

    reply
    0
  • typecho

    typecho2017-06-08 11:05:12

    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;

    reply
    0
  • Cancelreply