Home > Web Front-end > JS Tutorial > Javascript implements code to highlight keywords in the page_javascript skills

Javascript implements code to highlight keywords in the page_javascript skills

WBOY
Release: 2016-05-16 16:53:29
Original
1079 people have browsed it
Copy code The code is as follows:










How to obtain information. The word "Baidu" comes from the line "Searching for others in thousands of Baidu" in "The Blue Jade Case·Yuan Xi" by the Chinese Song Dynasty poet Xin Qiji, symbolizing Baidu's persistent pursuit of Chinese information retrieval technology.



<script> <br>function highlight(idVal, keyword) { <br>var textbox = document.getElementById(idVal); <br>if (" " == keyword) return; <br>//Get all text content<br>var temp = textbox.innerHTML; <br>console.log(temp); <br>var htmlReg = new RegExp("<.* ?>", "i"); <br>var arr = new Array(); <br><br>//Replace HTML tag <br>for (var i = 0; true; i ) { <br> //Match html tag <br>var tag = htmlReg.exec(temp); <br>if (tag) { <br>arr[i] = tag; <br>} else { <br>break; <br> } <br>temp = temp.replace(tag, "{[(" i ")]}"); <br>} <br><br><br>// Split keywords into arrays<br>words = decodeURIComponent(keyword.replace(/,/g, ' ')).split(/s /); <br><br>//Replace keywords<br>for (w = 0; w < words .length; w ) { <BR>// Match keywords and retain special characters that can appear in keywords <BR>var r = new RegExp("(" words[w].replace(/[(){}. *?^$|\[]]/g, "\$&") ")", "ig"); <BR>temp = temp.replace(r, "<b style='color:Red;' >$1</b>"); <br>} <br><br>//Restore HTML tag<br>for (var i = 0; i < arr.length; i ) { <BR>temp = temp.replace("{[(" i ")]}", arr[i]); <BR>} <BR>textbox.innerHTML = temp; <BR>} <BR>highlight("textbox"," Baidu, Robin Li"); <BR></script>


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template