简体   繁体   English

如何根据插入位置突出显示文本?

[英]How to highlight text based on caret position?

I want to have a button that adds a highlight to the currently selected text. 我想要一个按钮,为当前选定的文本添加一个突出显示。 I plan on doing this with the caret position of the selection. 我打算用选择的插入位置来做这件事。 I have the following code: 我有以下代码:

 function getCaretCharacterOffsetWithin(element) { var caretOffset = 0; var doc = element.ownerDocument || element.document; var win = doc.defaultView || doc.parentWindow; var sel; if (typeof win.getSelection != "undefined") { sel = win.getSelection(); if (sel.rangeCount > 0) { var range = win.getSelection().getRangeAt(0); var preCaretRange = range.cloneRange(); preCaretRange.selectNodeContents(element); preCaretRange.setEnd(range.endContainer, range.endOffset); caretOffset = preCaretRange.toString().length; } } else if ( (sel = doc.selection) && sel.type != "Control") { var textRange = sel.createRange(); var preCaretTextRange = doc.body.createTextRange(); preCaretTextRange.moveToElementText(element); preCaretTextRange.setEndPoint("EndToEnd", textRange); caretOffset = preCaretTextRange.text.length; } return caretOffset; } function showCaretPos() { var el = document.getElementById("test"); var caretPosEl = document.getElementById("caretPos"); caretPosEl.innerHTML = "Caret position: " + getCaretCharacterOffsetWithin(el); } document.body.onkeyup = showCaretPos; document.body.onmouseup = showCaretPos; 
 Non-editable text. Editable is below: <div id="test" contenteditable="true">Hello, some <b>bold</b> and <i>italic and <b>bold</b></i> text</div> <div id="caretPos"></div> 

The above code finds the caret position. 上面的代码找到了插入位置。 How can I now use the caret position to highlight only that position of text. 我现在如何使用插入位置仅突出显示文本的位置。

Possible Idea Get caret position from above and now substr() to add highlight. 可能的想法从上面获取插入位置,现在从substr()添加高亮显示。 str.substr(above caret position) . str.substr(above caret position)

What is the best way to accomplish this? 完成此任务的最佳方法是什么?

EDIT 编辑

I don't want to change the color selection. 我不想改变颜色选择。 I only want to use the caret position to then add a div that acts as a highlighter. 我只想使用插入位置然后添加一个充当荧光笔的div。 For example: 例如:

Hi, my name is Bob. 嗨,我叫鲍勃。

Now, I use the caret position to track the position for only my text selection. 现在,我使用插入符号位置来仅跟踪我的文本选择的位置。 Now, I replace the text with the original text but also with a div for the text selection. 现在,我将文本替换为原始文本,但也使用div替换文本选择。

Let's say I want highlight the phrase name is . 假设我要突出显示短语名称 I'm using this method of using caret position to not highlight every instance of this phrase. 我正在使用这种使用插入位置的方法来不突出显示该短语的每个实例。 Only the exact position of where the phrase is. 只有短语的确切位置。 For example, let's my text has the phrase name is twice, it will only highlight the phrase that matches the caret position. 例如,让我的文本的短语名称是两次,它只会突出显示与插入符号位置匹配的短语。 Then, replace the text with something like: <div id='highlight'> name is </div> 然后,用以下内容替换文本: <div id='highlight'> name is </div>

In the following Snippet are 2 functions that do the same thing except that the first one uses a <span> and the second one uses a <div> . 在下面的代码片段中有2个函数执行相同的操作,除了第一个使用<span>而第二个使用<div> What the functions do is essentially... 这些功能基本上是......

  1. ...removes the selected content (text)... ...删除所选内容(文字)......
  2. ...creates a <span> or <div> ... ...创建<span><div> ...
  3. ...sets the backgroundColor to yellow... ...将backgroundColor设置为黄色...
  4. ...the content is then appended to the <span/div> ... ...然后将内容附加到<span/div> ...
  5. ...then finally the <span/div> is inserted into the range . ...然后最后将<span/div>插入range

The <span> as you can see is probably a better choice. 您可以看到<span>可能是更好的选择。 Using a <div> is just plain messy. 使用<div>只是简单的混乱。

SNIPPET SNIPPET

 var spanEdit = document.getElementById('spanEdit'); var divEdit = document.getElementById('divEdit'); function highlightSpan() { var range = document.getSelection().getRangeAt(0); var contents = range.extractContents(); var node = document.createElement('span'); node.style.backgroundColor = "yellow"; node.appendChild(contents); range.insertNode(node); } function highlightDiv() { var range = document.getSelection().getRangeAt(0); var contents = range.extractContents(); var node = document.createElement('div'); node.style.backgroundColor = "yellow"; node.appendChild(contents); range.insertNode(node); } spanEdit.onkeyup = highlightSpan; spanEdit.onmouseup = highlightSpan; divEdit.onkeyup = highlightDiv; divEdit.onmouseup = highlightDiv; 
 <div id='spanEdit' contenteditable="true">Highlight inserted span. This uses extractContents() method.</div> <div>&nbsp;</div> <div id='divEdit' contenteditable="true">Highlight inserted div. This uses extractContents() method.</div> 

The functions are an adaptation from this post 这些功能是这篇文章的改编

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

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