简体   繁体   中英

How to prevent execution of onmousedown and onmouseup of div using javascript?

I have a div.

<div id="down" 
onmousedown="mousedown('url(/scanToUserBox/img/cpt_subfunc_005_next_d.png)', this)" 
onmouseup="mouseup('url(/scanToUserBox/img/cpt_subfunc_005_next_i.png)', this)" 
onclick="changePage('down')">
</div>

I want to prevent the execution of the events in some instances. I already tried disabling the div but still the events are executed.

Why can't you use JQuery and attach events based on actions? Are you restricted to javascript? Javascript Example:

document.getElementById('down').addEventListener('mousemove', elementMove, false);

Use JQuery, and attach functions to mouseup and mousedown event in which you can execute code depending on conditions:

$("#down").mouseup(function(){
  if(a=b) {

  }
}).mousedown(function(){
  if(b=c) {

  }
});

I believe, if you had added your event Handler simply via javascript with something like

var desDiv = document.getElementById('down');
desDiv.addEventListener('mousedown', mymousedownfn, false);

//you could have removed the eventlistener with
desDiv.removeEventListener('mousedown',mymousedownfn,false);

As you have added the eventlistener as an attribute of the div, I think the javascript becomes part of the attribute value as well and you would then need to set it to null to remove it. Something like

var desDiv = document.getElementById('down'); 
destdiv.onmousedown = null; 

You have to figure out when to set the 'destdiv.onmousedown' to null to make it work for you

I finally found the solution. It is event.returnValue = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM