JavaScript - 获取contentEditable div中当前行的HTML

时间:2017-09-16 12:25:49

标签: javascript jquery html

我的 contentEditable div

div {
  width: 200px;
  height: 300px;
  border: 1px solid black;
}
<div contenteditable="true" spellcheck="false" style="font-family: Arial;">
    <b>Lorem ipsum</b> dolor sit amet, <u>consectetur adipiscing elit. 
    Morbi sagittis</u> <s>mauris porta arcu auctor, vel aliquam ligula ornare.</s> 
    Sed at <span id="someId">semper neque, et dapibus metus. 
    Maecenas dignissim est non nunc feugiat</span> 
    sollicitudin. Morbi consequat euismod consectetur. Mauris orci 
    risus, <b>porta quis erat ac, malesuada</b> fringilla odio.
</div>
    
<button>Get current line HTML</button>

我想创建一个按钮,它给出了当前行的HTML代码。例如:
当我的插入符号位于第二行时,我想得到:

amet, <u>consectetur</u>

或在第七行:

<span id="someId">dapibus metus. Maecenas</span>

我尝试使用Rangy执行此操作,但这不起作用。我怎么能用JavaScript和/或JQuery做到这一点? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:8)

我不会为您编写完整的代码,但这里有很好的帮助,可以帮助您获得结果。

首先,您需要获得一种计算线条的方法。我建议在这个stackoverflow中查看答案:How to select nth line of text (CSS/JS) 从那里你可以获得特定单词的行号。

您的问候语可以从这些信息中获得:How can I get the word that the caret is upon inside a contenteditable div?

将行号功能与当前插入符号组合在一起,您将能够返回符号所在的完整行。

答案 1 :(得分:4)

此解决方案基于Mozilla在Selection.modify()中提出的示例,但使用lineboundary粒度并使用moveextend更改参数。

为了保留插入位置,存储/恢复选择的范围。

使用内容的width进行游戏,编辑代码段并将其检出。

你得到了你的HTML。

function getSelectionHtml() {
  var selection = window.document.selection,
    range, oldBrowser = true;

  if (!selection) {
    selection = window.getSelection();
    range = selection.getRangeAt(0);
    oldBrowser = false;
  } else
    range = document.selection.createRange();

  selection.modify("move", "backward", "lineboundary");
  selection.modify("extend", "forward", "lineboundary");

  if (oldBrowser) {
    var html = document.selection.createRange().htmlText;
    range.select();
    return html;
  }

  var html = document.createElement("div");

  for (var i = 0, len = selection.rangeCount; i < len; ++i) {
    html.appendChild(selection.getRangeAt(i).cloneContents());
  }

  selection.removeAllRanges();
  selection.addRange(range);
  return html.innerHTML;
}

document.getElementById("content").onmouseup = function(e) {
  var html = getSelectionHtml();
  document.getElementById("text").innerHTML = html;
  document.getElementById("code").textContent = html;
};
h4,
p {
  margin: 0;
}

#code {
  width: 100%;
  min-height: 30px;
}

#content {
  margin: 15px;
  padding: 2px;
  width: 200px;
  height: 300px;
  border: 1px solid black;
}
<textarea id="code"></textarea>
<div id="text"></div>

<div contenteditable="true" id="content" spellcheck="false" style="font-family: Arial;">
  <b>Lorem ipsum</b> dolor sit amet, <u>consectetur adipiscing elit. 
    Morbi sagittis</u> <s>mauris porta arcu auctor, vel aliquam ligula ornare.</s> Sed at <span id="someId">semper neque, et dapibus metus. 
    Maecenas dignissim est non nunc feugiat</span> sollicitudin. Morbi consequat euismod consectetur. Mauris orci risus, <b>porta quis erat ac, malesuada</b> fringilla odio.
</div>

相关问题