开发者

Preserving newlines and carriage returns from a selection using range? javascript / jquery

开发者 https://www.devze.com 2023-03-22 23:35 出处:网络
I have the following code if(this.window.getSelection) { var r开发者_开发问答ange = this.range; var contents = range.extractContents();

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

精彩评论

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

关注公众号