IE JS: how to use pasteHTML() when selection.type == 'None'?
I'm trying to allow the user to edit a contenteditable div, b开发者_如何学Pythonut am finding that I can't use pasteHTML unless there is some text selected.
I thought document.selection.createRange() would return a valid zero-length selection (i.e. a position), but alas no.
I've really struggled to find any solution to this that doesn't involve iframes (not an option at present).
Any suggestions/ideas/questions most welcome.
Make sure the focus is on the editable div before creating the TextRange
from the selection:
var div = document.getElementById("your_div");
div.focus();
document.selection.createRange().pasteHTML("<b>PASTED</b>");
You could use a form textarea. This is simple and cross-browser, but don't know to what extent your styling is limited or whether using a form or form element is appropriate for your issue.
精彩评论