当文本从 MS Word 粘贴到文本区域时,如何捕获格式?
我想实现一个简单的(至少看起来是这样)功能:当用户粘贴从 MS Word 或 Open Office writer 复制的文本时,我想将所有已设为粗体的文本换行> 在复制的文档中的 标记中。
例如,如果 Word 文档如下所示:
Lorem ipsum dolor sat amet,consectetur adipiscing elit。克拉斯韦利特 mi,facilisis sodales facilisis sat amet,vehicula vitae urna。塞德 arcu velit,eleifend et luctus a,gravida vel nulla。达皮布斯 enim发酵直径rutrum id hendreritelittincidunt。南恩尼安特, feugiat pretium iaculis id, luctus sat amet diam。
然后,如果用户将其复制并粘贴到文本区域,粘贴的文本将如下所示:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras velit
mi, facilisis sodales <b>facilisis</b> sit amet, vehicula vitae urna. Sed
arcu velit, eleifend et luctus a, gravida vel nulla. Etiam dapibus
enim fermentum diam rutrum id hendrerit <b>elit</b> tincidunt. Nam enim ante,
feugiat pretium iaculis id, luctus sit amet diam.
我知道 livejournal.com 曾经有此功能,所以它绝对是可能的。使用javascript/jquery实现这一点的原理是什么?
PS 我知道有所见即所得的编辑器,但它们对于我想做的事情来说大多是矫枉过正。
I want to implement a simple (at least it looks this way) functionality: when user is pasting a text, copied from MS word or Open office writer, I want to wrap all the text that have been made bold in the copied document in <b></b>
tags.
For example, if Word document looks like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras velit
mi, facilisis sodales facilisis sit amet, vehicula vitae urna. Sed
arcu velit, eleifend et luctus a, gravida vel nulla. Etiam dapibus
enim fermentum diam rutrum id hendrerit elit tincidunt. Nam enim ante,
feugiat pretium iaculis id, luctus sit amet diam.
Then, if user copies and pastes it into text area, pasted text will look like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras velit
mi, facilisis sodales <b>facilisis</b> sit amet, vehicula vitae urna. Sed
arcu velit, eleifend et luctus a, gravida vel nulla. Etiam dapibus
enim fermentum diam rutrum id hendrerit <b>elit</b> tincidunt. Nam enim ante,
feugiat pretium iaculis id, luctus sit amet diam.
I know livejournal.com used to have this functionality, so it's definetly possible. What are the principles of achieving this using javascript/jquery?
P.S. I am aware of WYSIWYG editors, but they are mostly an overkill for what I'm trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看这篇文章,其中详细介绍了如何做您正在寻找的事情。这样做似乎有几个注意事项,但是 Snook 设法得到了一个不错的例子。 http://snook.ca/archives/javascript/copying_from_mi
从链接:
使用 JavaScript 从 Microsoft Word 复制到文本区域
“我做了一些研究,发现没有多少人详细说明了执行此操作的解决方案,因此我创建了自己的解决方案。首先,有几件事这是一个基于客户端的解决方案,因此您的客户端必须在其计算机上安装 Microsoft Word,此解决方案才能访问客户端计算机上的文件,因此您的客户端必须具有允许的安全设置。脚本未标记为安全工作......”
Check out this article which details how to possibly do what you're looking for. It seems there are several caveats to doing this, however Snook managed to get a decent example working. http://snook.ca/archives/javascript/copying_from_mi
From the link:
Copying from Microsoft Word to a textarea using JavaScript
"I did some research and found that not many people had detailed a solution to do this so I've created my own solution. First, there's a couple things that you need to be aware of before you proceed. This is a client-based solution and therefore your client must have Microsoft Word installed on their machine. This solution accesses files on the client's machine and therefore your client must have their security settings to allow script not marked as safe to work..."