防止默认动作。仅在铬工作 [英] Prevent the default action. Working only in chrome

查看:122
本文介绍了防止默认动作。仅在铬工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jquery:

$(document).ready(function () {
   $('input[type=file]').uploadImage();
   jQuery.event.props.push('dataTransfer');
   $(".upload-cont").bind('dragenter', function (e) {
      $(".upload-cont").css("border", "1px dashed black;");
   });
   $(".upload-cont").bind('drop', function (e) {
      var files = e.dataTransfer.files;
      e.preventDefault();
   });
   $("body").bind('drop', function (e) {
      e.preventDefault();
   });
});

在firefox 17中,浏览器8将文件从桌面拖放到浏览器时,图像将被加载在另一页。我已经添加了在Chrome中完美工作的preventDefault()。可以做什么,以便在ff(即浏览器)中阻止该操作。

In firefox 17,explorer 8 when dragging and dropping a file from desktop into browser the image will be loaded in another page. I had added preventDefault() which works perfectly in chrome. What can be done so that the action is prevented in ff,ie browser.

推荐答案

尝试更改代码。

$('body').on('dragover drop', function(e){
    e.preventDefault();
});

拖动事件也必须取消,某些浏览器可以收听掉。引用 mdn

The dragover event has to be cancelled also for certain browser to listen to the drop. To quote the mdn


拖放事件的侦听器用于指示
有效的放置目标,即拖放的项目可能会被
丢弃的地方。网页或应用程序的大部分区域都不是有效的地方
删除数据。因此,这些事件的默认处理是不要
允许删除。

A listener for the dragenter and dragover events are used to indicate valid drop targets, that is, places where dragged items may be dropped. Most areas of a web page or application are not valid places to drop data. Thus, the default handling for these events is to not allow a drop.

如果要允许删除,则必须防止以
取消活动。您可以通过从
返回一个属性定义的事件侦听器,或通过调用事件的
event.preventDefault方法来执行此操作。在单独的脚本中定义的
函数中,后者可能更为可行。

If you want to allow a drop, you must prevent the default handling by cancelling the event. You can do this either by returning false from an attribute-defined event listener, or by calling the event's event.preventDefault method. The latter may be more feasible in a function defined in a separate script.

这篇关于防止默认动作。仅在铬工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆