admin管理员组文章数量:1428481
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.
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 1elements[0] is my entire page scope, so anywhere you click, it will trigger.
- Have a look - w3schools./jsref/tryit.asp?filename=tryjsref_onmousedown – SK. Commented Jul 23, 2015 at 13:24
4 Answers
Reset to default 1I'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;
本文标签: javascriptI39m trying to force mouseup event after mousedownStack Overflow
版权声明:本文标题:javascript - I'm trying to force mouseup event after mousedown - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745528896a2661968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论