explorer: how to highlight text after using pasteHTML method?
i replace selected text with pasteHTML method (Pastes HTML text into the given text range, replacing any previous text and HTML elements in the range.) in internet explorer.
var ran = Editor.selection.createRange();
ran.pasteHTML('<span style="font-size:20px;">example</span>');
after replacing text, selection dissapears. how to highlight 开发者_JS百科previous selection (text) again ? thanks
pasteHTML
will delete the current selection from the document, so I assume you want to select the span you've replaced it with. To do this, add an ID to the span, move the TextRange to encompass its text and select it, as follows:
var ran = Editor.selection.createRange();
ran.pasteHTML('<span style="font-size:20px;" id="a_random_unique_id">example</span>');
var spanRange = ran.duplicate();
spanRange.moveToElementText( document.getElementById("a_random_unique_id") );
spanRange.select();
精彩评论