1

我有一个 Ember 应用程序(Ember 2.6),我试图在将粘贴数据粘贴到输入组件之前捕获粘贴数据。我尝试按照文档在我的 Ember 应用程序中注册自定义事件。

https://emberjs.com/api/ember/2.15/classes/Ember.Application/properties/customEvents?anchor=customEvents&show=inherited%2Cprotected%2Cprivate%2Cdeprecated

然后在我的组件中...

paste: function(event) {
    console.log(event.originalEvent.clipboardData.getData('text/plain'))
}

粘贴“foo”时返回一个空字符串。

我也用jquery试过这个......

$('.table').on('paste',function(event) {
  console.log(event.originalEvent.clipboardData.getData('text/plain'))
}

这也返回一个空字符串。

我已阅读有关此问题的其他堆栈溢出答案,但尚未找到适合我的解决方案。我努力了...

event.clipboardData.getData('text/plain')
window.clipboardData..getData('text/plain')

对于 getData() 我也尝试过...

'text', and 'Text'

我该怎么办?

4

1 回答 1

1

您的里程可能会有所不同,但我正在捕获这样的粘贴事件:

if (event.originalEvent.clipboardData) {
  items = event.originalEvent.clipboardData.getData('Text').split('\r');
} else {
  items = window.clipboardData.getData('Text').split('\r');
}

假设我的split用例是多行粘贴(因此每行都是不同的项目),但如果粘贴中没有多行,它应该工作相同。

于 2018-01-12T21:53:03.647 回答