开发者

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:

  1. Connect the textbox to a onkeypress event
  2. In the event function, do an filter for each word that is typed, and match it to words in the div.
  3. For each matched word, replace the word in the div with <span style="background-color:yellow;">word</span> by modifying the content of one.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜