久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

通過 dataTransfer 傳遞對象

Pass object through dataTransfer(通過 dataTransfer 傳遞對象)
本文介紹了通過 dataTransfer 傳遞對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試找出一種通過 javascript 的 event.dataTransfer 傳遞原生對象以進行拖放的方法.我正在編寫 CMS 的前端編輯器部分,并希望用戶能夠拖放元素(許多不同類型的元素,從文件到圖像到 HTML 片段到幾乎任何東西).這就是為什么我想傳遞一個真實的對象,所以我可以將數據附加到它以指定為了知道如何呈現它而需要的東西.

I'm trying to figure out a way to pass a native object through javascript's event.dataTransfer for drag and drop. I'm writing the front end editor portion of a CMS, and want users to be able to drag and drop elements (of many different types, ranging from files to images to snippets of HTML to just about anything). That's why I want to pass a real object, so I can attach data to it to specify things that will be necessary in order to know how to render it.

一種解決方法是使用 jQuery 的 .data() 將對象附加到頁面上的其他內容,然后簡單地在 setData 中傳遞選擇器字符串...但我并不特別喜歡這種 hack.

One workaround is to use jQuery's .data() to attach an object to something else on the page, and then simply pass the selector string in setData... but I don't particularly enjoy that hack.

這也可以通過 jQuery UI 的可拖放/可拖放來解決(我并不完全反對使用任何庫),但由于 dropzone 位于 iframe 中,這實際上是最新 jQuery UI 中記錄的錯誤(1.10.2).另外,如果可能的話,我強烈希望在本地進行.此應用中的庫已經足夠了.

This could also be solved with jQuery UI's draggable/droppable (and I'm not totally opposed to using any libraries), but since the dropzone is in an iframe, this is a actually a documented bug in the latest jQuery UI (1.10.2). Also, I would strongly prefer to do it natively if possible. There are already enough libraries in this app.

問題出在這里:http://jsfiddle.net/AHnh8/

var foo = {foo: "foo", bar: "bar"};

$dragme.on("dragstart", function(e) {
    e.originalEvent.dataTransfer.setData("foo", foo);
});

$dropzone.on("dragenter", function(e) { e.preventDefault(); });
$dropzone.on("dragover", function(e) { e.preventDefault(); });
$dropzone.on("drop", function(e) {
    console.log(e.originalEvent.dataTransfer.getData("foo")); // [Object object]
    console.log(typeof e.originalEvent.dataTransfer.getData("foo")); // string
});

現在,在閱讀了規范之后,我完全明白為什么會失敗.我不明白的是,如何最終實現我想要的.

Now, after reading the specs, I totally understand why this fails. What I don't understand, is how to ultimately achieve what I want.

謝謝

推薦答案

你應該在使用 setData 之前將對象傳遞給 JSON.stringify 因為你只能存儲字符串.

You should pass the object to JSON.stringify before using setData because you can only store strings.

var j = JSON.stringify(foo);
e.originalEvent.dataTransfer.setData("foo", j);

反之,你必須將字符串重新轉換為對象:

And the other way round you have to reconvert the string to an object:

var obj = JSON.parse(e.originalEvent.dataTransfer.getData("foo"));
console.log("foo is:", obj);

查看 this 工作小提琴

這篇關于通過 dataTransfer 傳遞對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
backbone.js click event spy is not getting called using jasmine.js and sinon.js(沒有使用 jasmine.js 和 sinon.js 調用主干.js 點擊事件間諜)
How do I write FileReader test in Jasmine?(如何在 Jasmine 中編寫 FileReader 測試?)
Stub out a jQuery selector call?(存根一個 jQuery 選擇器調用?)
jQuery trigger(#39;click#39;) not working with Jasmine-jquery(jQuery 觸發器(click)不適用于 Jasmine-jquery)
How to test the done and fail Deferred Object by using jasmine(如何使用 jasmine 測試完成和失敗的延遲對象)
主站蜘蛛池模板: 国外成人在线视频网站 | 日韩国产一区二区三区 | 亚洲aⅴ一区二区 | 成人精品一区亚洲午夜久久久 | 成人国产精品免费观看 | 日本免费在线观看视频 | 伊人精品在线视频 | 先锋资源在线 | www.v888av.com| 色噜噜色综合 | 国产操操操 | 精品乱码久久久久 | 国产精品免费看 | 一级片网站视频 | 麻豆成人在线视频 | 亚洲在线日韩 | 久久国产欧美日韩精品 | 国产激情毛片 | 天天干天天干 | 欧洲精品码一区二区三区免费看 | 亚洲天堂一区 | 天堂久 | 99爱在线视频 | 国产 欧美 日韩 一区 | 中文字幕第100页 | 一区二区三区国产好 | 九九久久精品视频 | 欧美久久一区 | 久久久久久国 | 日韩精品一区二区三区久久 | 人人看人人干 | 色呦呦网站 | 国产在线a| 亚洲性人人天天夜夜摸 | 天天干夜夜操视频 | 91色在线视频 | 欧美成人一区二区三区 | 国产一级片在线观看视频 | 夜久久| 色吧综合网 | 国产欧美久久精品 |