change color of a word in div value using javascript
I have a <div id="one">this is a sentence</div>
I have a textbox for userinput, if the user types a word which m开发者_如何学Pythonatches any word in div, eg: if user types "this", which is also there in div value, the background color of "this" should be changed, using Javascript
Thanks Sunny.
Try this:
- Connect the textbox to a onkeypress event
- In the event function, do an filter for each word that is typed, and match it to words in the div.
- For each matched word, replace the
word
in the div with<span style="background-color:yellow;">word</span>
by modifying the content ofone.innerHTML
.
Note, before you modify the div, you should store the original content in a global variable that is used as base for the matching, otherwise, the matching will be cluttered with the <span...>
tags.
精彩评论