問題描述
我在 Google 和 SO 上進行了快速搜索,發現了類似的問題,但沒有一個問題是正確的,而且大多數都是舊的,看起來被遺棄了(沒有答案,有一段時間沒有評論).所以就到這里了……
I ran a quick google and SO search and found similar questions but none were well formed and most were old and looked abandoned (no answers, and no comments for a while). So here goes...
我希望能夠收集從另一個網站拖放到我的網站上的圖像的 url(僅 url)..(即我打開了兩個 chrome 窗口.窗口 A 中有我的應用程序.窗口 B 有imgur 在其中.我打開一個圖像單擊并將其拖動到我的窗口并松開.現在我需要知道放置在我的頁面上的圖像的 url).
I want to be able to collect the url (only the url) of an image being dropped onto my site from another website.. (i.e. I have two chrome windows open. Window A has my application in it. Window B has imgur in it. I open an image click and drag it to my window and let go. Now I need to know the url of the image dropped on my page).
這是我用于本地文件的代碼.
Here is the code I was working with for local files.
$(document).on('drop', function(e) {
var data = e.dataTransfer || e.originalEvent.dataTransfer;
console.log(data); // data.files is empty
e.preventDefault();
return false;
});?
再次,我不想上傳任何東西..我不想做任何花哨的事情...我只需要知道從另一個網站拖放到頁面上的圖像的位置.
Again I do not want to upload anything.. i'm not trying to do anything fancy... I just need to know the location of the image being dropped on the page from another website.
推薦答案
試試這個:http://jsfiddle.net/2Jet2/70/
$(document).on('dragover', function(e) {
e.preventDefault();
});
$(document).on('drop', function(e) {
e.preventDefault();
e.originalEvent.dataTransfer.items[0].getAsString(function(url){
alert(url);
});
});?
我得到 "http://static3.flattr.net/thing/image/9/4/5/5/0/huge.png?1326712342"
當我從另一個拖動該圖像時瀏覽器窗口.
I get "http://static3.flattr.net/thing/image/9/4/5/5/0/huge.png?1326712342"
When I dragged that image from another browser window.
.getAsString
接受一個回調,一旦它被調用,它就會獲取 url 作為參數
.getAsString
takes a callback which gets the url as argument once it's called
無法在火狐上運行
這篇關于從另一個網站拖放圖像到我的的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!