Clipboard

更新时间:
复制 MD 格式

Use my.getClipboard and my.setClipboard to read and write clipboard data in mini programs.

my.getClipboard

Note

Supported in mPaaS 10.1.32 and later.

Retrieves clipboard data.

Request parameters

Name

Type

Required

Description

success

Function

No

Callback on success.

fail

Function

No

Callback on failure.

complete

Function

No

Callback that runs when the call completes, regardless of success or failure.

Success return value

Name

Type

Description

text

String

Clipboard data.

Code examples

// API-DEMO page/API/clipboard/clipboard.json
{
  "defaultTitle": "Clipboard"
}
<!-- API-DEMO page/API/clipboard/clipboard.axml-->
<view class="page">
  <view class="page-section">
    <view class="page-section-title">setClipboard</view>
    <view class="page-section-demo">
      <input onInput="handleInput" value="{{text}}" />
      <button class="clipboard-button" type="primary" size="mini" onTap="handleCopy">Copy</button>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">getClipboard</view>
    <view class="page-section-demo">
      <input onInput="bindInput" value="{{copy}}" disabled />
      <button class="clipboard-button" type="default" size="mini" onTap="handlePaste">Paste</button>
    </view>
  </view>
</view>
// API-DEMO page/API/clipboard/clipboard.js
Page({
  data: {
    text: '3.1415926',
    copy: '',
  },

  handleInput(e) {
    this.setData({
      text: e.detail.value,
    });
  },

  handleCopy() {
    my.setClipboard({
      text: this.data.text,
    });
  },

  handlePaste() {
    my.getClipboard({
      success: ({ text }) => {
        this.setData({ copy: text });
      },
    });
  },
});
/* API-DEMO page/API/clipboard/clipboard.acss */
.clipboard-button {
  margin-left: 5px;
}

my.setClipboard

Note

Supported in mPaaS 10.1.32 and later.

Sets clipboard data.

Request parameters

Name

Type

Required

Description

text

String

Yes

Data to write to the clipboard.

success

Function

No

Callback on success.

fail

Function

No

Callback on failure.

complete

Function

No

Callback that runs when the call completes, regardless of success or failure.

Code examples

// API-DEMO page/API/clipboard/clipboard.json
{
  "defaultTitle": "Clipboard"
}
<!-- API-DEMO page/API/clipboard/clipboard.axml-->
<view class="page">
  <view class="page-section">
    <view class="page-section-title">setClipboard</view>
    <view class="page-section-demo">
      <input onInput="handleInput" value="{{text}}" />
      <button class="clipboard-button" type="primary" size="mini" onTap="handleCopy">Copy</button>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">getClipboard</view>
    <view class="page-section-demo">
      <input onInput="bindInput" value="{{copy}}" disabled />
      <button class="clipboard-button" type="default" size="mini" onTap="handlePaste">Paste</button>
    </view>
  </view>
</view>
// API-DEMO page/API/clipboard/clipboard.js
Page({
  data: {
    text: '3.1415926',
    copy: '',
  },

  handleInput(e) {
    this.setData({
      text: e.detail.value,
    });
  },

  handleCopy() {
    my.setClipboard({
      text: this.data.text,
    });
  },

  handlePaste() {
    my.getClipboard({
      success: ({ text }) => {
        this.setData({ copy: text });
      },
    });
  },
});
/* API-DEMO page/API/clipboard/clipboard.acss */
.clipboard-button {
  margin-left: 5px;
}