在javascript中从剪贴板获取html
我需要实现 RichTextEditors 非常常见的功能 - 从剪贴板中获取 HTML 。 任何人都可以帮助指导如何解决此任务吗?
它必须是跨平台的(IE、FF、Chrome、Opera)。 我刚刚从这段代码开始:
<script type="text/javascript">
$('.historyText').live('input paste', function(e) {
var paste = e.clipboardData && e.clipboardData.getData ?
e.clipboardData.getData('text/plain') : // Standard
window.clipboardData && window.clipboardData.getData ?
window.clipboardData.getData('Text') : // MS
false;
alert(paste);
});</script>
window.clipboardData 和 e.clipboardData 均为 null(Chrome、Firefox)。
更新:用户想要从其他浏览器窗口粘贴文章内容,我需要获取 html 标签。
I need to implement task which is quite common feature for RichTextEditors - take HTML from clipboard.
Can anyone help with guide on how to solve this task?
It has to be cross platform (IE, FF, Chrome, Opera).
I just started from this code:
<script type="text/javascript">
$('.historyText').live('input paste', function(e) {
var paste = e.clipboardData && e.clipboardData.getData ?
e.clipboardData.getData('text/plain') : // Standard
window.clipboardData && window.clipboardData.getData ?
window.clipboardData.getData('Text') : // MS
false;
alert(paste);
});</script>
Both window.clipboardData and e.clipboardData are null (Chrome, Firefox).
Update: User wants to paste article content from other browser windows, and I need to get html tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我实际上已经在这方面做了很多工作,刚刚写了一个 不错的博客文章描述了我们如何在 Lucidchart 详细地做到这一点(作为免责声明,我在 Lucidchart 工作)。我们有 一个 JSFiddle,可以显示复制和粘贴(在 Firefox、Safari、Chrome 和 IE9+ 中测试)。
简而言之,您需要在系统粘贴事件期间获取 HTML。在大多数(非 IE)浏览器中,这可以通过如下简单的操作来完成:
问题是当您想在 IE 中获取 HTML 时。无论出于何种原因,IE 都无法通过 JavaScript 访问文本/html 剪贴板数据。你要做的就是让浏览器粘贴到一个contenteditable div,然后在粘贴事件结束后获取html。
I actually have done a lot of work on this, and just wrote a nice blog post describing how we did it in detail at Lucidchart (as a disclaimer, I work at Lucidchart). We have a JSFiddle that shows copying and pasting (tested in Firefox, Safari, Chrome, and IE9+).
The short of the answer is that you will need to get the HTML during the system paste event. In most (non-IE) browsers, this can be done with something as simple as the following:
The problem is when you want to get HTML in IE. For whatever reason, IE doesn't make the text/html clipboard data accessible via javascript. What you have to do is let the browser paste to a contenteditable div and then get the html after the paste event is over.
您将无法单独使用 JavaScript 从剪贴板获取数据,而这本来就是应该的方式。当前版本的 TinyMCE 和 CKEditor 执行此操作的方式如下:
请注意,这仅适用于键盘粘贴事件,不适用于从上下文或编辑菜单进行粘贴。当粘贴事件触发时,将插入符号重定向到 div 中已经太晚了(至少在某些浏览器中)。
You won't be able to get data from the clipboard using JavaScript alone, which is the way it should be. The way current versions of TinyMCE and CKEditor do this is as follows:
Note that this will only work for keyboard paste events and not pastes from the context or edit menus. By the time the paste event fires, it's too late to redirect the caret into the div (in some browsers, at least).
在 Chrome 中,我使用以下代码通过事件访问 ClipboardData:
In Chrome, I access clipboardData through the event using this code:
这段代码为我完成了这项工作。
有两件事要记住。
This piece of code did the job for me.
2 things to keep in mind.