最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Drop Event Not Preventing Default - Stack Overflow

programmeradmin0浏览0评论

I've the following: /

function dragEnter(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).addClass('over');
}

function dragLeave(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).removeClass('over');
}

function drop(evt) {
   evt.stopPropagation();
   evt.preventDefault();
   $(evt.target).removeClass('over');
}

jQuery( function ( $ ) {

  var $box = $( "#box" );
  $box.bind("dragenter", dragEnter);
  $box.bind("dragleave", dragLeave);
  $box.bind("drop", drop);
});

I'm using Chrome version 24.0.1312.52 m and last jQuery (1.8.3). When I drop a file into the box, browser is not preventing default beaviour. Can you please help me?

P.S. dragexit is deprecated correct?

I've the following: http://jsfiddle.net/KywJT/

function dragEnter(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).addClass('over');
}

function dragLeave(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).removeClass('over');
}

function drop(evt) {
   evt.stopPropagation();
   evt.preventDefault();
   $(evt.target).removeClass('over');
}

jQuery( function ( $ ) {

  var $box = $( "#box" );
  $box.bind("dragenter", dragEnter);
  $box.bind("dragleave", dragLeave);
  $box.bind("drop", drop);
});

I'm using Chrome version 24.0.1312.52 m and last jQuery (1.8.3). When I drop a file into the box, browser is not preventing default beaviour. Can you please help me?

P.S. dragexit is deprecated correct?

Share Improve this question edited Nov 4, 2019 at 19:33 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jan 14, 2013 at 20:00 user1824269user1824269 6331 gold badge9 silver badges18 bronze badges 4
  • 1 It works for me and I didn't modify a single line of code – Alexander Commented Jan 14, 2013 at 20:05
  • 2 Oo.. It was just working until I updated Chrome :| Version 24.0.1312.52 m now it just opens the file in the browser – wirey00 Commented Jan 14, 2013 at 20:09
  • K, I'm not crazy. Any fix? – user1824269 Commented Jan 14, 2013 at 20:14
  • @user1824269 I'm not sure.. still playing around with it. It was just working, was going to paste my Chrome browser version.. But it said I need to restart browser to update.. Once I restarted it didn't work anymore.. – wirey00 Commented Jan 14, 2013 at 20:18
Add a comment  | 

3 Answers 3

Reset to default 17

If you want the drop event to be fired in Google Chrome, a dragover listener has to be defined before, even if the function doesn't do anything at all. I don't know why, it's weird... but clearly identified and easy to reproduce :) Lost 2 hours on that sh*t yesterday :/ hope this helps

This should fix the issue for you.

jQuery( function ( $ ) {
    var $box = $( "#box" );
    $box.bind("dragenter", dragEnter);
    $box.bind("dragleave", dragLeave);
    $box.bind("drop", drop);

    $(document).bind('dragover', function (e) {
         e.preventDefault();
    });
});

This is an old question but I used answers to this question and wrote the code I want:

HTML:

<div id="image_holder" ondragover="dragEnter(this,event)"></div>

jQuery:

function dragEnter(a,evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).addClass('over');

    $(a).bind("dragleave", dragLeave);
    $(a).bind("drop", drop);

    $(document).bind('dragover', function (e) {
        e.stopPropagation();
        e.preventDefault();
    });
    return false;
}

function dragLeave(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).removeClass('over');
    return false;
}

function drop(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).removeClass('over');
}

This has the privilege you can use it easily for different elements in page.

发布评论

评论列表(0)

  1. 暂无评论