javascript - I'm trying to force mouseup event after mousedown - Stack Overflow

elements[0].onmousedown = function(){console.log('screen clicked.');simulatemouseuphere;};Al

elements[0].onmousedown = function(){
      console.log('screen clicked.');
      simulatemouseuphere;
};

All I need to do is add something in my function that triggers mouseup, even if the user is holding the click button down.

elements[0] is my entire page scope, so anywhere you click, it will trigger.

elements[0].onmousedown = function(){
      console.log('screen clicked.');
      simulatemouseuphere;
};

All I need to do is add something in my function that triggers mouseup, even if the user is holding the click button down.

elements[0] is my entire page scope, so anywhere you click, it will trigger.

Share Improve this question edited Jul 23, 2015 at 13:34 abejdaniels asked Jul 23, 2015 at 13:21 abejdanielsabejdaniels 4596 silver badges16 bronze badges 1
  • Have a look - w3schools./jsref/tryit.asp?filename=tryjsref_onmousedown – SK. Commented Jul 23, 2015 at 13:24
Add a ment  | 

4 Answers 4

Reset to default 1

I'm pretty sure you can just call the onmouseup event...

elements[0].onmousedown = function(){
      console.log('screen clicked.');

      elements[0].onmouseup();
};

Keep in mind that when you actually physically stop holding the mouse down though, the mouseup event will be triggered again. (unless in the onmouseup() event you manage that)

Tested quickly, looks like it works: http://jsfiddle/cUCWn/40/

$("body").on("mousedown", function(e)
{
    e.type = "mouseup";
    $(this).trigger(e);
}).on("mouseup", function(e)
{
    console.info(e);
});

Or: https://stackoverflow./a/12724570/2625561

For pure JS: https://developer.mozilla/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Triggering_built-in_events

Just a guess, but instead of firing a mouseup event, perhaps you want to trigger the function that is bound to the mouseup event?

var onMouseUp = function() { 
 /* something */  
};

var onMouseDown = function() { 
 /* something */ 
 onMouseUp.apply(this,arguments); 
};

elements[0].onmousedown = onMouseDown;
elements[0].onmouseup = onMouseUp;

All the ways that are described here depend on how you set up the listener: jQuery, element property.

If you want a robust way that will work regardless of how the event was setup, use How can I trigger a JavaScript event click

elements[0].onmousedown = function(){
    fireEvent(elements[0], 'onmouseup');
};

Notice that you are probably better off wrapping the behavior of the mouseup handler into a function that you can call. Triggering handlers is usually code smell because you don't always know what else you may be triggering when firing events. For example:

function doSomethingOnMouseUp() {
    console.log('something on mouse up');
}

elements[0].onmousedown = function(){
    doSomething();
    doSomethingOnMouseUp();
};

elements[0].onmouseup = doSomethingOnMouseUp;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745528897a4631602.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信