JavaScript操作剪贴板 - ( for IE )

本文详细介绍了如何使用clearDataMethod从剪贴板中移除指定格式的数据,如文本、URL等;getDataMethod用于获取剪贴板中特定格式的数据;setDataMethod则用于向剪贴板设置指定格式的数据。这些方法在拖拽操作及跨域数据传输中有重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bRet = [window.]clipboardData.clearData(['Text' | 'URL' | 'File' | 'HTML' | 'Image'])
sData = [window.]clipboardData.getData('Text' | 'URL')
bAdded = [window.]clipboardData.setData('Text' | 'URL', sData)

 

 

下面为来自MSDN的内容:

参见:

http://msdn.microsoft.com/en-us/vstudio/ms536352%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/vstudio/ms536436%28v=VS.85%29

http://msdn.microsoft.com/en-us/vstudio/ms536744%28v=VS.85%29

 

clearData Method

Removes one or more data formats from the clipboard through the dataTransfer object or the clipboardData object.

Syntax

        pret = object.clearData( [sDataFormat])

Parameters

    sDataFormat    Optional. A String that specifies one or more of the following data format values.

    Text
        Removes the text format.
    URL
        Removes the URL format.
    File
        Removes the file format.
    HTML
        Removes the HTML format.
    Image
        Removes the image format.

Return Value

    A Boolean value indicating success or failure.
Remarks

    If no sDataFormat parameter is passed, the data formats are cleared.

    For drag-and-drop operations, the clearData method of the dataTransfer object is used generally in source events, such as ondragstart. When you override the default behavior of the target, use clearData in the ondrop event. It is particularly useful for selectively removing data formats when multiple formats are specified.
   
getData Method

Gets the data in the specified format from the clipboard through the dataTransfer object or the clipboardData object.

Syntax

        sRetrieveData = object.getData(sDataFormat)

Parameters

    sDataFormat    Required. A String that specifies one of the following data format values.

    Text
        Gets data formatted as text.
    URL
        Gets data formatted as a URL.

Return Value

    String. Returns the data in the format retrieved from the clipboard through the dataTransfer object or the clipboardData object. Depending on the information contained in setData, this variable can get a path to an image, text, or an anchor URL.

Remarks

    The getData method enforces cross-frame security and allows data transfers only in the same domain. To the user, this means that a selection that is dragged between different security protocols, such as HTTP and HTTPS, fails. In addition, that a selection that is dragged between two instances of the application with different security levels, where the first instance is set to medium and the second is set to high, fails. Finally, that a selection that is dragged into the application from another drag-enabled application, such as Microsoft Word, also fails.

    To use the getData method to get data from the clipboard in the oncopy event or the oncut event, specify window.event.returnValue=false in the event handler script.


setData Method

Assigns data in a specified format to the dataTransfer object or the clipboardData object.

Syntax

        bSuccess = object.setData(sDataFormat, sData)

Parameters

    sDataFormat    Required. A String that specifies the format of the data to be transferred, using one of the following values.

    Text
        Transfers data formatted as text.
    URL
        Transfers data formatted as a URL.

    sData    Required. A String that specifies the data supplied by the source object. This information can be descriptive text, a source path to an image, or a URL for an anchor. When you pass "URL" as the sDataFormat parameter, you must use the sData parameter to provide the location of the object that is transferred.

Return Value

    Boolean. Returns one of the following possible values.

    true
        The data was successfully added.
    false
        The data was not added.

Remarks

    The value of the sDataFormat parameter is not case-sensitive.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值