如何在“drop"事件后停止重定向?

2023-02-24前端开发问题
2

本文介绍了如何在“drop"事件后停止重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Firefox 中将文件放入 div 后,网页将被重定向到该文件.我试图在 drop 处理程序中使用 jQuery 的 e.preventDefault() 来阻止这种传播,但失败了.

After dropping a file into a div in Firefox, the webpage will be redirected to this file. I tried to stop this propagation using jQuery's e.preventDefault() in drop handler, and failed.

请参阅此演示,将文件放入 #test 不会't 重定向网页,但放入 #test1 会,我想知道为什么.我是否应该始终将处理程序绑定到 dragenterdragoverdragleavedrop 以防止在 drop<之后传播/代码>?

See this demo, dropping a file into #test won't redirect the webpage, but dropping into #test1 will, I want to know why. Should I always bind handlers to dragenter, dragover, dragleave and drop to prevent propagation after drop?

更新:

我在 html5doctor 上找到了一些提示:

I found some tips on html5doctor:

要告诉浏览器我们可以放入这个元素,我们所要做的就是取消拖动事件.然而,由于 IE 的行为不同,我们需要对 dragenter 事件做同样的事情.

To tell the browser we can drop in this element, all we have to do is cancel the dragover event. However, since IE behaves differently, we need to do the same thing for the dragenter event.

和 Mozilla 声明:

And Mozilla claims:

dragenterdragover 事件的监听器用于指示有效的放置目标,即拖动项目可能的位置被丢弃.

A listener for the dragenter and dragover events are used to indicate valid drop targets, that is, places where dragged items may be dropped.

但我在 firefox 上测试了 这个演示,#test 有效并且#test1 没有,看来 Mozilla 犯了一个错误,而 html5doctor 是对的:Firefox 只需要 dragover 才能使 drop 工作.

But I test this demo on firefox, #test works and #test1 doesn't, seems Mozilla made a mistake, and html5doctor is right: Firefox needs dragover only to make drop work.

推荐答案

ondragover 事件需要在 Google ChromeSafari 中取消允许触发 ondrop 事件.

The ondragover event needs to be canceled in Google Chrome and Safari to allow firing the ondrop event.

这篇关于如何在“drop"事件后停止重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5