Preserving newlines and carriage returns from a selection using range? javascript / jquery
I have the following code
if(this.window.getSelection) {
var r开发者_开发问答ange = this.range;
var contents = range.extractContents();
console.log(contents);
return contents.textContent;
}
contents gives me a document fragment. When I check the text content of that fragment it collapses the newlines in my selection into a single string.
How do I preserve the formatting of the string?
you can use contents.innerHTML
if your contents
is a html element, or contents.html()
if it is a jQuery object.
ps: is this.window.getSelection
not supposed to be this.window.getSelection()
?
精彩评论