如何从所选文本中复制格式? [英] How to copy formatting from the selected text?

查看:129
本文介绍了如何从所选文本中复制格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个令我困惑的用例。当我选择一些文本并将其粘贴到Google的任何应用程序(如文档或笔记本)中时,它会以某种方式设法将文本粘贴到其原始格式,有时会粘贴图像本身。有人可以告诉我如何使用javascript / jquery做到这一点?

Here's one usecase that has been baffling me. When I select some text and paste it in any of Google's applications like docs or notebook, it somehow manages to paste the text with its original formatting and sometimes images itself. Can someone tell me how to do this using javascript/jquery?

编辑:
我正在考虑的另一个方案是设计基于服务器的复制/粘贴机制。

One more scenario that I am looking at is say designing a server-based copy/paste mechanism.

推荐答案

你可以使用 contenteditable 执行此操作:

You can use contenteditable to do this:

<div contenteditable="true">Stuff pasted in here will retain their original HTML</div>

这是一个简单的演示: http://jsfiddle.net/AA3Kq/

Here's a simple demo for this: http://jsfiddle.net/AA3Kq/

关于你的第二种情况,你会想看看JavaScript的选择API。我不是这方面的专家,但是这里有一些你可以开始的地方:

Regarding your second scenario, you'll want to look at JavaScript's selection API. I'm not an expert on this, but here's some places where you can get started on:

  • http://www.quirksmode.org/dom/range_intro.html
  • http://blogs.msdn.com/b/ie/archive/2010/05/11/dom-range.aspx
  • http://code.google.com/p/rangy/ and http://code.google.com/p/ierange/

这篇关于如何从所选文本中复制格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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