--- title: DataTransfer slug: Web/API/DataTransfer translation_of: Web/API/DataTransfer ---
{{ gecko_minversion_header("1.9") }} El objeto DataTransfer
es usado como contenedor de datos que estan siendo manipulados durante la operación de drag and drop. Este puede contener uno o varios objetos, de uno o varios tipos de datos. Para más información en cuanto a drag and drop, ver Drag and Drop.
Este objeto esta disponible desde la propiedad dataTransfer
de todos los eventos de drag . No pueden ser creados por separado.
Properties | Type |
dropEffect |
String |
effectAllowed |
String |
files {{ gecko_minversion_inline("1.9.2") }} |
{{ domxref("FileList") }} |
mozCursor {{ non-standard_inline() }} {{ gecko_minversion_inline("1.9.1") }} |
String |
mozItemCount {{ non-standard_inline() }} |
unsigned long |
mozSourceNode {{ non-standard_inline() }} {{ gecko_minversion_inline("2") }} |
{{ domxref("Node") }} |
mozUserCancelled |
Boolean |
types |
DOMStringList |
void addElement(in Element element) |
void clearData([in String type]) |
String getData(in String type) |
void setData(in String type, in String data) |
void setDragImage(in nsIDOMElement image, in long x, in long y) |
void mozClearDataAt([in String type, in unsigned long index]) |
nsIVariant mozGetDataAt(in String type, in unsigned long index) |
void mozSetDataAt(in String type, in nsIVariant data, in unsigned long index) |
StringList mozTypesAt([in unsigned long index]) |
The actual effect that will be used, and should always be one of the possible values of effectAllowed
.
For the dragenter
and dragover
events, the dropEffect
will be initialized based on what action the user is requesting. How this is determined is platform specific, but typically the user can press modifier keys to adjust which action is desired. Within an event handler for the dragenter
and dragover
events, the dropEffect
should be modified if the action the user is requesting is not the one that is desired.
For dragstart
, drag
, and dragleave
events, the dropEffect
is initialized to "none". Any value assigned to the dropEffect
will be set, but the value isn't used for anything.
For the drop
and dragend
events, the dropEffect
will be initialized to the action that was desired, which will be the value that the dropEffect
had after the last dragenter
or dragover
event.
Possible values:
Assigning any other value has no effect and retains the old value.
Specifies the effects that are allowed for this drag. You may set this in the dragstart
event to set the desired effects for the source, and within the dragenter
and dragover
events to set the desired effects for the target. The value is not used for other events.
Possible values:
Assigning any other value has no effect and retains the old value.
{{ gecko_minversion_header("1.9.2") }}
Contains a list of all the local files available on the data transfer. If the drag operation doesn't involve dragging files, this property is an empty list. An invalid index access on the {{ domxref("FileList") }} specified by this property will return null
.
This example dumps the list of files dragged into the browser window: http://jsfiddle.net/9C2EF/
Holds a list of the format types of the data that is stored for the first item, in the same order the data was added. An empty list will be returned if no data was added.
{{ gecko_minversion_note("1.9.2", 'The string "Files" is included in this list if files are included in the drag.') }}
{{ h3_gecko_minversion("mozCursor", "1.9.1", "mozCursor") }}
The drag cursor's state. This is primarily used to control the cursor during tab drags.
auto
default
The number of items being dragged.
{{ gecko_minversion_header("2") }}
The {{ domxref("Node") }} over which the mouse cursor was located when the button was pressed to initiate the drag operation. This value is null
for external drags or if the caller can't access the node.
This property applies only to the dragend
event, and is true
if the user canceled the drag operation by pressing escape. It will be false
in all other cases, including if the drag failed for any other reason, for instance due to a drop over an invalid location. This property is not currently implemented on Linux.
Set the drag source. Usually you would not change this, but it will affect which node the drag and dragend events are fired at. The default target is the node that was dragged.
void addElement( in Element element );
element
Remove the data associated with a given type. The type argument is optional. If the type is empty or not specified, the data associated with all types is removed. If data for the specified type does not exist, or the data transfer contains no data, this method will have no effect.
void clearData( [optional] in String type );
type
Retrieves the data for a given type, or an empty string if data for that type does not exist or the data transfer contains no data.
A security error will occur if you attempt to retrieve data during a drag that was set from a different domain, or the caller would otherwise not have access to. This data will only be available once a drop occurs during the drop event.
String getData( in String type );
type
Set the data for a given type. If data for the type does not exist, it is added at the end, such that the last item in the types list will be the new format. If data for the type already exists, the existing data is replaced in the same position. That is, the order of the types list is not changed when replacing data of the same type.
void setData( in String type, in String data );
type
data
Set the image to be used for dragging if a custom one is desired. Most of the time, this would not be set, as a default image is created from the node that was dragged.
If the node is an HTML img element, an HTML canvas element or a XUL image element, the image data is used. Otherwise, image should be a visible node and the drag image will be created from this. If image is null, any custom drag image is cleared and the default is used instead.
The coordinates specify the offset into the image where the mouse cursor should be. To center the image, for instance, use values that are half the width and height of the image.
void setDragImage( in Element image, in long x, in long y );
image
x
y
Removes the data associated with the given format for an item at the specified index. The index is in the range from zero to the number of items minus one.
If the last format for the item is removed, the entire item is removed, reducing mozItemCount
by one.
If the format
list is empty, then the data associated with all formats is removed. If the format is not found, then this method has no effect.
void mozClearDataAt( [optional] in String type, in unsigned long index );
type
index
Retrieves the data associated with the given format for an item at the specified index, or null if it does not exist. The index should be in the range from zero to the number of items minus one.
nsIVariant mozGetDataAt( [optional] in String type, in unsigned long index );
type
index
A data transfer may store multiple items, each at a given zero-based index. mozSetDataAt()
may only be called with an index argument less than mozItemCount
in which case an existing item is modified, or equal to mozItemCount
in which case a new item is added, and the mozItemCount
is incremented by one.
Data should be added in order of preference, with the most specific format added first and the least specific format added last. If data of the given format already exists, it is replaced in the same position as the old data.
The data should be either a string, a primitive boolean or number type (which will be converted into a string) or an {{ interface("nsISupports") }}.
void mozSetDataAt( [optional] in String type, in nsIVariant data, in unsigned long index );
type
data
index
Holds a list of the format types of the data that is stored for an item at the specified index. If the index is not in the range from 0 to the number of items minus one, an empty string list is returned.
nsIVariant mozTypesAt( in unsigned long index );
index
{{ languages( { "ja": "Ja/DragDrop/DataTransfer" } ) }}