問題描述
在 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
會,我想知道為什么.我是否應該始終將處理程序綁定到 dragenter
、dragover
、dragleave
和 drop
以防止在 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:
dragenter
和 dragover
事件的監聽器用于指示有效的放置目標,即拖動項目可能的位置被丟棄.
A listener for the
dragenter
anddragover
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 Chrome 和 Safari 中取消允許觸發 ondrop
事件.
The ondragover
event needs to be canceled in Google Chrome and Safari to allow firing the ondrop
event.
這篇關于如何在“drop"事件后停止重定向?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!