开发者

How to re-trigger (or late-trigger) a `mousedown` event in jQuery?

开发者 https://www.devze.com 2023-01-31 08:09 出处:网络
One question bugs my mind lately: In a mousedown handler I use .preventDefault on the event to prevent 开发者_如何学JAVAtext selection while dragging:

One question bugs my mind lately: In a mousedown handler I use .preventDefault on the event to prevent 开发者_如何学JAVAtext selection while dragging:

$(document).bind('mousedown', function(event){
    event.preventDefault();
});

So far so good.

Then, while still having the mouse button pressed down, I would like to wait for a longclick event to be triggered (http://github.com/pisi/Longclick) and have the longclick handler to somehow resume the original mousedown event and commence the text selection like if the event's default wasn't ever prevented.

Is there a way to kind of late-trigger an otherwise native mouse event?

Is .trigger capable to somehow accept an existing (stored) event object? For example something like this?:

var originalEvent;

$(document)
    .bind('mousedown', function(event){
        event.preventDefault();
        originalEvent= event;
    })
    .bind('longclick', function(event){
        $(event.target).trigger(originalEvent)
    })

I'm after being able to drag an entire text paragraph and still offer the possibility to perform text selection within the paragraph.

All answers, suggestions and thoughts are welcome!


Passing an event object to trigger works as far as jQuery is concerned (event handlers bound with jQuery will be executed, and receive the object as a parameter), which allows you to do cool stuff like this, but no default action will take place. Most browsers support the DOM level 2 specification, dispatchEvent, except for the usual suspect, which has fireEvent instead, which is pretty useless as it does not trigger the default action.

0

精彩评论

暂无评论...
验证码 换一张
取 消