简体   繁体   English

如何突出显示文本框中的文本?

[英]How can I highlight the text In the textbox?

The code for text area. 文本区域的代码。

<div>
      <textarea id="ta" onpaste="functionFind(event)"></textarea>
</div>

The function that will be executed 将要执行的功能

function functionFind(pasteEvent)
{    
    var textareacont = (pasteEvent.originalEvent || pasteEvent).clipboardData.getData("text/html");
    var tofindword = prompt("Enter the word to find");
    if(textareacont.includes(tofindword ))
    {
        //What code do I write here so that all the word that matches "tofindword" gets highlighted in the same textbox only
    }
}

The function will be executed once we paste something into the textbox and all the matching words should get highlighted in the same textbox only. 一旦我们将某些内容粘贴到文本框中,所有匹配的单词应仅在同一文本框中突出显示,该函数将被执行。

You can't actually render markup inside a textarea. 您实际上无法在文本区域内渲染标记。 However, you can fake it by 但是,您可以通过以下方式伪造它

  • carefully positioning a div behind the textarea 小心地将div放置在文本区域的后面
  • Keep the div's inner HTML same as of textarea 保持div的内部HTML与textarea相同
  • Add your highlight markup to the div 将突出显示标记添加到div

For example: 例如:

<div class="container">
    <div class="backdrop">
    <div class="highlights">
            <mark>This</mark> demo shows how to highlight bits of text within a textarea. <mark>Alright</mark>, that's a lie. <mark>You</mark> can't actually render markup inside a textarea. <mark>However</mark>, you can fake it by carefully positioning a div behind the textarea and adding your highlight markup there. <mark>JavaScript</mark> takes care of syncing the content and scroll position from the textarea to the div, so everything lines up nicely. <mark>Hit</mark> the toggle button to peek behind the curtain. <mark>And</mark> feel free to edit this text. <mark>All</mark> capitalized words will be highlighted.
        </div>
    </div>
<textarea>
    This demo shows how to highlight bits of text within a textarea. Alright, that's a lie. You can't actually render markup inside a textarea. However, you can fake it by carefully positioning a div behind the textarea and adding your highlight markup there. JavaScript takes care of syncing the content and scroll position from the textarea to the div, so everything lines up nicely. Hit the toggle button to peek behind the curtain. And feel free to edit this text. All capitalized words will be highlighted.</textarea>
</div>

And style the mark tag 并标记标记样式

mark {
  border-radius: 3px;
  color: transparent;
  background-color: #b1d5e5;
}

For your requirement, 根据您的要求

  • Add css for mark tag 为标记标签添加CSS
  • Add a div behind your text area. 在您的文本区域后面添加一个div。
  • while 'onpaste' in textarea, copy the content of text area into div's innerHTML 在textarea中“ onpaste”时,将文本区域的内容复制到div的innerHTML中
  • Search for the text from the prompt in the div and add mark tag for it 从div的提示中搜索文本并为其添加标记标签

Refer the codepen link for details 请参阅代码笔链接以获取详细信息

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

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