简体   繁体   English

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

[英]JavaScript - Get HTML of current line in contentEditable div

I have got my contentEditable div : 我有我的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> 

And I want to create a button which gives me HTML code from the current line. 我想创建一个按钮,从当前行给我HTML代码。 For example: 例如:
When my caret is on the second line I want to get: 当我的插入符号位于第二行时,我想得到:

amet, <u>consectetur</u>

or on seventh line: 或在第七行:

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

I tried to do that using Rangy , but that didn't work. 我试图用Rangy做到这一点 ,但那没用 How can I do that using JavaScript and/or JQuery? 我怎么能用JavaScript和/或JQuery做到这一点?
Thanks for help. 感谢帮助。

I will not code you the full finished code but here are good help that will get you the result. 我不会为您编写完整的代码,但这里有很好的帮助,可以帮助您获得结果。

First you need to get a method for figuring out lines. 首先,您需要一种方法来计算线条。 I suggest looking at the answer in this stackoverflow: How to select nth line of text (CSS/JS) From that you can get line number for a specific word. 我建议在这个stackoverflow中查看答案: 如何选择第n行文本(CSS / JS)从中可以得到特定单词的行号。

What word your caret is on you can get from this information: How can I get the word that the caret is upon inside a contenteditable div? 您的插入符号对您来说可以从这些信息中得到什么: 如何在一个可疑的div中获得插入符号所在的单词?

Combining the line number functionality with current caret word you will be able to return a full row where your caret is. 将行号功能与当前插入符号组合在一起,您将能够返回符号所在的完整行。

This solution is based on the example proposed by Mozilla in Selection.modify() , but using lineboundary granularity and playing with move and extend alter parameters. 此解决方案基于Mozilla在Selection.modify()提出的示例,但使用行lineboundary粒度并使用moveextend alter参数。

To preserve caret position, the range of the selection is stored/restored. 为了保持插入位置,存储/恢复选择范围。

Play with the width of the content, edit the snippet and check it out. 播放内容的width ,编辑片段并检查它。

You got your HTML. 你有你的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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM