-2

I am wondering how to search specific string in big textbox (which contains 200 words) so I can make function to color them. Ex. In textbox there is a sentence "my dog is happy" and i want string "dog" to become red by button or sth else. Is it possible???

4

1 回答 1

1

对的,这是可能的。但不要使用文本框或文本区域,使用divwith contenteditable = "true"

<div id="editableDiv" class="editable" contenteditable="true">
    This is a sentence containing 'dog'.<br />
    You can edit the contents of this div.
</div>
<button id="highlightBtn">Highlight "dog"</button>
<script type="text/javascript">
   highlightBtn.onclick = function() {
         var elem = document.getElementById('editableDiv');
         elem.innerHTML = elem.innerHTML.replace(/dog/g, 
                            '<span class="redText">dog</span>');
   }
 </script>

redText并且不要忘记editable在样式表中创建类:

.editable {
    padding: 5px;
    border: dashed 1px black;
}
.redText {
    color: red;
}

JSFiddle:http: //jsfiddle.net/ProgramFOX/UMMPh/

于 2013-09-04T17:22:53.120 回答