开发者

Delete a div with the right mouseclick (block contextmenu)

开发者 https://www.devze.com 2023-02-20 18:43 出处:网络
i have a problem with the right-click-contextmenu i try to delete a div $(\".global_class\").live(\'mousedown\', function(e)

i have a problem with the right-click-contextmenu

i try to delete a div

$(".global_class").live('mousedown', function(e)
{
    if( (e.which == 3) ) 
    {
       // $('#'+this.id+'').remove();
       del_function(this.id);
    }
    e.preventDefault();
}).live('contextmenu', function(e){  e.preventDefault(); });

This code works but the problem is the $('#'+this.id+'').remove();

jquery removes the div and the live $(".global_class").live('mousedown', function(e) didn't start (result -> the context menu will not be blocked).

Hope someone can help me.

Thanks in advance!

Pet开发者_StackOverflow社区er


When you remove the elements that are involved in an event, results in terms of bubbling and defaults and such get a bit squirrelly (even without jQuery's live being involved). You could do this:

$(".global_class").live('mousedown', function(e)
{
    var id = this.id;

    if( (e.which == 3) ) 
    {
       setTimeout(function() {
            del_function(id);
       }, 0);
    }
    e.preventDefault();
}).live('contextmenu', function(e){  e.preventDefault(); });

...so that you remove the div immediately after the event finishes, rather than during the event.


Off-topic, but you can't cancel the contextmenu event on all browsers (Opera comes to mind), which you may want to factor into your UI decisions...

0

精彩评论

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

关注公众号