我怎样才能得到将被粘贴在我的HTML文本编辑器中的文本? [英] How can I get the text that is going to be pasted in my html text editor?

查看:205
本文介绍了我怎样才能得到将被粘贴在我的HTML文本编辑器中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的主页(textarea)中有一个小文本编辑器,可以粘贴文本。如何在粘贴之前将这些文本放入变量中?我知道有这个函数:

$ p $ clipboardData.getData()

但它在Firefox中不起作用,我想要所有浏览器的东西。我使用的是HTML和Javascript。



感谢

解决方案

简短的回答是,在粘贴文本之前, 。你可以做的是什么大的基于网页的所见即所得的编辑器,如CKEditor和TinyMCE做的,它是:
$ b


  1. 检测一个 Ctrl使用按键事件处理程序 - v / shift -
  2. 在该处理程序中,保存当前的用户选择,在屏幕外添加一个< div> 元素(比如在左边-1000px )移动到文档中,将插入符号移动到该div的内部,从而有效地重定向粘贴。

  3. 在事件处理函数中设置一个非常简短的计时器(比如1毫秒),以调用另一个函数来自div的HTML内容并执行所需的任何处理,从文档中移除div,恢复用户选择并插入处理后的HTML。

请注意,这只适用于键盘粘贴事件,不适用于上下文或编辑菜单。在粘贴事件发生的时候,将脱字符重定向到div(至少在某些浏览器中)是太迟了。

I have a small text editor in my home page ( a textarea ) where I can paste text. How can I get this text in a variable before it would be pasted? I know that there is this function:

clipboardData.getData()

but it doesn't work in Firefox, and I want something for all the browsers.I am using HTML and Javascript.

thanks

解决方案

The short answer is that in general you can't get the text before it is pasted. What you can do is what big web-based WYSIWYG editors such as CKEditor and TinyMCE do, which is:

  1. Detect a Ctrl-v / shift-ins event using a keypress event handler
  2. In that handler, save the current user selection, add a <div> element off-screen (say at left -1000px) to the document, move the caret to be inside that div, thus effectively redirecting the paste
  3. Set a very brief timer (say 1 millisecond) in the event handler to call another function that retrieves the HTML content from the div and does whatever processing is required, removes the div from the document, restores the user selection and inserts the processed HTML.

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).

这篇关于我怎样才能得到将被粘贴在我的HTML文本编辑器中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆