Check if mousedown with an if statement?

前端 未结 5 641
悲哀的现实
悲哀的现实 2021-02-02 17:16

Is it possible to do something like this:

if ($(this).mousedown() == true) {

I thought that would work but it doesn\'t.

Additio

5条回答
  •  后悔当初
    2021-02-02 17:50

    Answering your original question, yes this is possible. When the mousedown event fires over an element the element becomes active and is selectable by the pseudo selector :active. In jQuery if nothing is selected an empty array is returned, we can use this to our advantage in combination with the .length array property and turn the active pseudo selector into a check to see if the mouse is down over a particular element or not like so:

    if ( $('#id:active').length ) {
      //do something
    }
    

    Checking if the mouse was down over a particular element at a given point in time is a different event and does not go with the title of this question and should be changed might I add. Google brought me here and this answer is for those that will inevitably follow.

提交回复
热议问题