开发者

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()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜