搜尋
首頁web前端html教學拖拽类型_html/css_WEB-ITnose

拖拽文本 EDIT

拖拽文本的时候,使用 text/plain类型。数据应该是被拖拽的字符串。例如:

event.dataTransfer.setData("text/plain", "This is text to drag")

拖拽网页上的文本框内文本及已选文本是自动完成的,所以你不需要自己处理。

建议你总是添加 text/plain类型数据作为不支持其它类型的应用或投放目标的降级,除非没有可选的符合逻辑的文本。总是将纯文本类型添加在最后,因为这是最不具体的数据( as it is the least specific)。

在旧的代码中,你可能会遇到 text/unicode或 Text类型。这些与 text/plain类型是等效的,存储、获取到的都是纯文本数据。

拖拽链接 EDIT

链接需要包含两种类型的数据;第一种是 text/uri-list类型的URL,第二种是 text/plain类型的URL。两种类型要使用相同的数据,即链接的URL。例如:

var dt = event.dataTransfer;dt.setData("text/uri-list", "http://www.mozilla.org");dt.setData("text/plain", "http://www.mozilla.org");

与之前一样,将 text/plain类型添加在最后,因为它不如uri类型具体。

注意URL类型是 uri-list,“uri”中包含的是“i”,而不是“l”。

拖拽多条链接时,你可以使用换行将每条链接分开。以井号(#)开头的行是注释,不应该认为是合法的URL。你可以使用注释来指明链接的含义,或保存与链接相关的标题。 text/plain版本的数据应该包含所有的链接但不应该包含注释。

例如:

http://www.mozilla.org#A second linkhttp://www.xulplanet.com

这个text/uri-list数据样例包含两条链接和一条注释。

当得到一条拖放的链接,你需要确保你能处理包含多条链接以及注释的数据。出于便利考虑,特殊类型 URL可以用来获得 text/uri-list类型数据中的第一条合法的链接(译注:chrome则是得到 text/uri-list的完整数据)。你不应该使用  URL类型来添加数据,这样做只是设置了  text/uri-list 类型的数据。

var url = event.dataTransfer.getData("URL");

你也可以使用Mozilla自定义的 text/x-moz-url类型。如果使用了,它需要添加在 text/uri-list类型之前。他保存链接的 URL,后面跟随链接的标题,之间只用断行隔开。例如:

http://www.mozilla.orgMozillahttp://www.xulplanet.comXUL Planet

拖拽HTML与XML EDIT

HTML内容可以使用 text/html类型。这种类型的数据需要是序列化的HTML。例如,使用元素的 innerHTML属性值来设置这个类型的值是合适的。

XML内容可以使用 text/xml类型,但你要确保数据值是格式良好的XML。

你也可以包含使用 text/plain类型表示的HTML或XML的纯文本。该数据应该只包含文本内容而不包含源标签或属性。例如:

var dt = event.dataTransfer;dt.setData("text/html", "Hello there, <strong>stranger</strong>");dt.setData("text/plain", "Hello there, stranger");

Dragging Files EDIT

A local file is dragged using the application/x-moz-filetype with a data value that is an  nsIFileobject. Non-privileged web pages are not able to retrieve or modify data of this type. Because a file is not a string, you must use the  mozSetDataAtmethod to assign the data. Similarly, when retrieving the data, you must use the  mozGetDataAtmethod.

event.dataTransfer.mozSetDataAt("application/x-moz-file", file, 0);

If possible, you may also include the file URL of the file using both the text/uri-listand/or  text/plaintypes. These types should be added last so that the more specific  application/x-moz-filetype has higher priority.

Multiple files will be received during a drop as mutliple items in the data transfer. See Dragging and Dropping Multiple Itemsfor more details about this.

The following example shows how to create an area for receiving dropped files:

<listbox ondragenter="return checkDrag(event)" ondragover="return checkDrag(event)" ondrop="doDrop(event)"/><script>function checkDrag(event){ return event.dataTransfer.types.contains("application/x-moz-file");}function doDrop(event){ var file = event.dataTransfer.mozGetDataAt("application/x-moz-file", 0); if (file instanceof Components.interfaces.nsIFile) event.currentTarget.appendItem(file.leafName);}</script>

In this example, the event returns false only if the data transfer contains the application/x-moz-filetype. During the drop event, the data associated with the file type is retrieved, and the filename of the file is added to the listbox. Note that the  instanceofoperator is used here as the mozGetDataAtmethod will return an  nsISupportsthat needs to be checked and converted into an nsIFile. This is also a good extra check in case someone made a mistake and added a non-file for this type.

Dragging Images EDIT

Direct image dragging is not commonly done. In fact, Mozilla does not support direct image dragging on Mac or Linux platforms. Instead, images are usually dragged only by their URLs. To do this, use the text/uri-listtype as with other URL links. The data should be the URL of the image or a data URL if the image is not stored on a web site or disk. For more information about data URLs, see  the data URL scheme.

As with other links, the data for the text/plaintype should also contain the URL. However, a data URL is not usually as useful in a text context, so you may wish to exclude the  text/plaindata in this situation.

In chrome or other privileged code, you may also use the image/jpeg,  image/pngor  image/giftypes, depending on the type of image. The data should be an object which implements the  nsIInputStreaminterface. When this stream is read, it should provide the data bits for the image, as if the image was a file of that type.

You should also include the application/x-moz-filetype if the image is located on disk. In fact, this a common way in which image files are dragged.

It is important to set the data in the right order, from most specific to least specific. The image type such as image/jpegshould come first, followed by the  application/x-moz-filetype. Next, you should set the  text/uri-listdata and finally the  text/plaindata. For example:

var dt = event.dataTransfer;dt.mozSetDataAt("image/png", stream, 0);dt.mozSetDataAt("application/x-moz-file", file, 0);dt.setData("text/uri-list", imageurl);dt.setData("text/plain", imageurl);

Note that the mozGetDataAtmethod is used for non-text data. As some contexts may only include some of these types, it is important to check which type is made available when receiving dropped images.

Dragging Nodes EDIT

Nodes and elements in a document may be dragged using the application/x-moz-nodetype. This data for the type should be a DOM node. This allows the drop target to receive the actual node where the drag was started from. Note that callers from a different domain will not be able to access the node even when it has been dropped.

You should always include a plain text alternative for the node.

Dragging Custom Data EDIT

You can also use other types that you make up for custom purposes. You should strive to always include a plain text alternative unless that object being dragged is specific to a particular site or application. In this case, the custom type ensures that the data cannot be dropped elsewhere.

Dragging files to an operating system folder EDIT

There are cases in which you may want to add a file to an existing drag event session, and you may also want to write the file to disk when the drop operation happens over a folder in the operating system when your code receives notification of the target folder's location. This only works in extensions (or other privileged code) and the data flavor "application/moz-file-promise" should be used. The following sample offers an overview of this advanced case:

// currentEvent is a given existing drag operation eventcurrentEvent.dataTransfer.setData("text/x-moz-url", URL);currentEvent.dataTransfer.setData("application/x-moz-file-promise-url", URL);currentEvent.dataTransfer.setData("application/x-moz-file-promise-filename", leafName);currentEvent.dataTransfer.mozSetDataAt('application/x-moz-file-promise',                  new dataProvider(success,error),                  0, Components.interfaces.nsISupports);function dataProvider(){} dataProvider.prototype = {  QueryInterface : function(iid) {    if (iid.equals(Components.interfaces.nsIFlavorDataProvider)                  || iid.equals(Components.interfaces.nsISupports))      return this;    throw Components.results.NS_NOINTERFACE;  },  getFlavorData : function(aTransferable, aFlavor, aData, aDataLen) {    if (aFlavor == 'application/x-moz-file-promise') {         var urlPrimitive = {};       var dataSize = {};         aTransferable.getTransferData('application/x-moz-file-promise-url', urlPrimitive, dataSize);       var url = new String(urlPrimitive.value.QueryInterface(Components.interfaces.nsISupportsString));       console.log("URL file orignal is = " + url);             var namePrimitive = {};       aTransferable.getTransferData('application/x-moz-file-promise-filename', namePrimitive, dataSize);       var name = new String(namePrimitive.value.QueryInterface(Components.interfaces.nsISupportsString));         console.log("target filename is = " + name);         var dirPrimitive = {};       aTransferable.getTransferData('application/x-moz-file-promise-dir', dirPrimitive, dataSize);       var dir = dirPrimitive.value.QueryInterface(Components.interfaces.nsILocalFile);         console.log("target folder is = " + dir.path);         var file = Cc['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);       file.initWithPath(dir.path);       file.appendRelativePath(name);         console.log("output final path is =" + file.path);         // now you can write or copy the file yourself...    }   }}
<strong>来源:https://developer.mozilla.org</strong>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
超越HTML:網絡開發的基本技術超越HTML:網絡開發的基本技術Apr 26, 2025 am 12:04 AM

要構建一個功能強大且用戶體驗良好的網站,僅靠HTML是不夠的,還需要以下技術:JavaScript賦予網頁動態和交互性,通過操作DOM實現實時變化。 CSS負責網頁的樣式和佈局,提升美觀度和用戶體驗。現代框架和庫如React、Vue.js和Angular,提高開發效率和代碼組織結構。

HTML中的布爾屬性是什麼?舉一些例子。HTML中的布爾屬性是什麼?舉一些例子。Apr 25, 2025 am 12:01 AM

布爾屬性是HTML中的特殊屬性,不需要值即可激活。 1.布爾屬性通過存在與否控制元素行為,如disabled禁用輸入框。 2.它們的工作原理是瀏覽器解析時根據屬性的存在改變元素行為。 3.基本用法是直接添加屬性,高級用法可通過JavaScript動態控制。 4.常見錯誤是誤以為需要設置值,正確寫法應簡潔。 5.最佳實踐是保持代碼簡潔,合理使用布爾屬性以優化網頁性能和用戶體驗。

如何驗證您的HTML代碼?如何驗證您的HTML代碼?Apr 24, 2025 am 12:04 AM

HTML代碼可以通過在線驗證器、集成工具和自動化流程來確保其清潔度。 1)使用W3CMarkupValidationService在線驗證HTML代碼。 2)在VisualStudioCode中安裝並配置HTMLHint擴展進行實時驗證。 3)利用HTMLTidy在構建流程中自動驗證和清理HTML文件。

HTML與CSS和JavaScript:比較Web技術HTML與CSS和JavaScript:比較Web技術Apr 23, 2025 am 12:05 AM

HTML、CSS和JavaScript是構建現代網頁的核心技術:1.HTML定義網頁結構,2.CSS負責網頁外觀,3.JavaScript提供網頁動態和交互性,它們共同作用,打造出用戶體驗良好的網站。

HTML作為標記語言:其功能和目的HTML作為標記語言:其功能和目的Apr 22, 2025 am 12:02 AM

HTML的功能是定義網頁的結構和內容,其目的在於提供一種標準化的方式來展示信息。 1)HTML通過標籤和屬性組織網頁的各個部分,如標題和段落。 2)它支持內容與表現分離,提升維護效率。 3)HTML具有可擴展性,允許自定義標籤增強SEO。

HTML,CSS和JavaScript的未來:網絡開發趨勢HTML,CSS和JavaScript的未來:網絡開發趨勢Apr 19, 2025 am 12:02 AM

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML:結構,CSS:樣式,JavaScript:行為HTML:結構,CSS:樣式,JavaScript:行為Apr 18, 2025 am 12:09 AM

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

HTML的未來:網絡設計的發展和趨勢HTML的未來:網絡設計的發展和趨勢Apr 17, 2025 am 12:12 AM

HTML的未來充滿了無限可能。 1)新功能和標準將包括更多的語義化標籤和WebComponents的普及。 2)網頁設計趨勢將繼續向響應式和無障礙設計發展。 3)性能優化將通過響應式圖片加載和延遲加載技術提升用戶體驗。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具