How to disable Firefox's default drag and drop on all images behavior with jQuery?

前端 未结 6 567
我寻月下人不归
我寻月下人不归 2021-02-05 10:51

Firefox has this annoying behavior that it let\'s the user drag and drop any image element by default. How can I cleanly disable this default behavior with jQuery?

6条回答
  •  情深已故
    2021-02-05 11:14

    The following will do it in Firefox 3 and later:

    $(document).on("dragstart", function() {
         return false;
    });
    

    If you would prefer not to disable all drags (e.g. you may wish to still allow users to drag links to their link toolbar), you could make sure only element drags are prevented:

    $(document).on("dragstart", function(e) {
         if (e.target.nodeName.toUpperCase() == "IMG") {
             return false;
         }
    });
    

    Bear in mind that this will allow images within links to be dragged.

提交回复
热议问题