简体   繁体   English

JavaScript; 突出显示文本区域中特定字符串的所有出现

[英]JavaScript; highlight all occurrences of a particular string in a textarea

Let's say I have this code:假设我有这个代码:

<textarea style="width:300px;height:200px">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ipsum eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ipsum ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in ipsum reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur ipsum sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit ipsum anim id est laborum.</textarea>

I want to highlight all occurrences of the string ipsum .我想突出显示所有出现的字符串ipsum I already googled but only found scripts that highlight a single occurrence only.我已经用谷歌搜索过,但只找到了仅突出显示单个事件的脚本。

Does anybody have a hint for me?有人给我提示吗?

You need to get a plugin that tokenize your textarea content into balise; 您需要获得一个插件,将您的textarea内容标记为balise; See http://codersblock.com/blog/highlight-text-inside-a-textarea/ 参见http://codersblock.com/blog/highlight-text-inside-a-textarea/

Thing is there is no way to highlight text in textarea , if you want to highlight text dynamically, as I assume because you use <textarea> , then you could use div with contenteditable 如您所愿,如果您想动态突出显示文本,则无法在textarea中突出显示文本,因为您使用<textarea> ,则可以将div与contenteditable一起使用。

<div style="width:300px;height:400px" id='text' contenteditable='true'>
     Lorem ipsum dolor sit amet....
</div>

add eventListener and replace every occurence of ipsum to <span>ipsum<span> on space key press. 添加eventListener并将每次出现的ipsum替换为按空格键的<span>ipsum<span>

var textElem = document.getElementById("text");

textElem.onkeyup = function(e) {
  if (e.keyCode == 32) { //check on space press 
    higlight(this);
  }
}

function higlight(element) {
  var text = element.innerHTML;
  var newString = text.replace(/ipsum/g, "<span class='highlight'>ipsum</span>");
  element.innerHTML = newString;
  setEndOfContenteditable(element); // set cursor at the end of contenteditable

}

Keep in mind that there may be more optimized solution, it depends on your requiremients. 请记住,可能会有更多优化的解决方案,具体取决于您的要求。 Here is preview: 这是预览:

 var textElem = document.getElementById("text"); textElem.onkeyup = function(e) { if (e.keyCode == 32) { //check on space press higlight(this); } } function higlight(element) { var text = element.innerHTML; var newString = text.replace(/ipsum/g, "<span class='highlight'>ipsum</span>"); element.innerHTML = newString; setEndOfContenteditable(element); } higlight(textElem); // to set cursor at the end of contentEditable function setEndOfContenteditable(contentEditableElement) { var range, selection; if (document.createRange) //Firefox, Chrome, Opera, Safari, IE 9+ { range = document.createRange(); //Create a range (a range is a like the selection but invisible) range.selectNodeContents(contentEditableElement); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start selection = window.getSelection(); //get the selection object (allows you to change selection) selection.removeAllRanges(); //remove any selections already made selection.addRange(range); //make the range you have just created the visible selection } else if (document.selection) //IE 8 and lower { range = document.body.createTextRange(); //Create a range (a range is a like the selection but invisible) range.moveToElementText(contentEditableElement); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start range.select(); //Select the range (make it the visible selection } } 
 .highlight { background: yellow; } #text { background: white; border: 1px solid black; } 
 <div style="width:300px;height:400px" id='text' contenteditable='true'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ipsum eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ipsum ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in ipsum reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur ipsum sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit ipsum anim id est laborum.</div> 

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

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