1

Делаю загрузку файлов через drag&drop

$('.new-pl-content')
    .on('dragover', function () {
        $(this).addClass('pl-content-over');
        $('span', this).hide();

    })
    .on('dragleave', function () {
        $(this).removeClass('pl-content-over');
        $('span', this).show();
    })
    .on('drop', function (e) {
        e.preventDefault();
    });

не срабатывает preventDefault и файл открывается ссылкой в браузере. В чем может быть проблема? Спасибо.

1
  • А сниппет добавить?
    – Qwertiy
    3 фев 2016 в 10:06

1 ответ 1

2

Ты должен отменить все события.

$("html").on("dragover", function(event) {
    event.preventDefault();  
    event.stopPropagation();
    $(this).addClass('dragging');
});

$("html").on("dragleave", function(event) {
    event.preventDefault();  
    event.stopPropagation();
    $(this).removeClass('dragging');
});

$("html").on("drop", function(event) {
    event.preventDefault();  
    event.stopPropagation();
    alert("Dropped!");
});
4
  • Вызвать всё что есть, так, на всякий случай?
    – Qwertiy
    3 фев 2016 в 10:45
  • 1
    @Qwertiy Можешь проверить каждый на его отменяемость event.originalEvent.cancelable; и убрать лишние event.preventDefault();. 3 фев 2016 в 10:50
  • 1
    Я про то, действительно ли для поставленной задачи нужно вызывать оба метода (preventDefault и stopPropagation) в каждом из трёх обработчиков?
    – Qwertiy
    3 фев 2016 в 10:56
  • 1
    @Qwertiy event.stopPropagation() блокирует последующие обработчики. Тут он необязателен. 3 фев 2016 в 10:59

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.