简体   繁体   English

document.execCommand(“ paste”)不执行

[英]document.execCommand(“paste”) doesn't execute

I have an input HTML element like so: 我有一个input HTML元素,如下所示:

<input title="clipboard" id="clipboard-input" type="text" value="">

I have binded it's paste event with jquery to a custom code which is not relevant but this binding works when ctrl+v is pressed while the element is focused. 我已经将它的粘贴事件与jquery绑定到一个不相关的自定义代码,但是当元素被聚焦时按下ctrl + v时,此绑定有效。

Now, I have created a button for triggering the paste event. 现在,我创建了一个用于触发粘贴事件的按钮。 It's just a simple button with click event bound to a function with body such as this: 这只是一个简单的button ,单击事件绑定到具有以下内容的函数:

$("#clipboard-input")[0].focus();
document.execCommand("paste");

The click event is fired when button is pressed, clipboard element get's focused, but the paste event is not triggered by execCommand . 单击事件会在单击按钮时触发click事件,从而使剪贴板元素获得焦点,但是粘贴事件并非由execCommand触发。 Reading the documentation this should work, as it is user-initiated action which is calling the execCommand but for some reason the execCommand returns false, meaning that it failed. 阅读文档应该可以,因为这是用户启动的操作,正在调用execCommand,但是由于某种原因execCommand返回false,这意味着它失败了。

I'm looking for a cross-browser solution to this problem without using flash. 我正在寻找一种跨浏览器解决方案来解决此问题,而不使用Flash。

Edit: 编辑:

I have created MWE (minimal non-working example) here . 我创建MWE(最小的非工作示例) 在这里 Try pasting something directly in the input field and the paste handler will be called, changing the text in paragraph. 尝试将某些内容直接粘贴到输入字段中,然后将调用粘贴处理程序,从而更改段落中的文本。 But when the button is pressed, the input field is focused but the execCommand fails to trigger the paste event. 但是,当按下按钮时,输入字段被聚焦,但是execCommand无法触发粘贴事件。

This is a specific command works only when the browser is in design mode that is the document is turned into an editor that exposes the RTE features and the exact syntax is 仅当浏览器处于设计模式(即将文档转换为可显示RTE功能的编辑器,并且确切的语法为

document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)

Ref : https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand 参考: https : //developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM