How to copy formatting from the selected text?
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?
开发者_JS百科Edit: One more scenario that I am looking at is say designing a server-based copy/paste mechanism.
You can use contenteditable
to do this:
<div contenteditable="true">Stuff pasted in here will retain their original HTML</div>
Here's a simple demo for this: http://jsfiddle.net/AA3Kq/
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/
精彩评论