The example in this article describes the method of JavaScript identifying web page keywords and rendering them. Share it with everyone for your reference, the details are as follows:
Here is a demonstration of JavaScript intelligently identifying web page keywords and displaying them in red. I believe this effect is familiar to everyone. Using JS to add red keywords is better than program-controlled output. Much more, some functions will be handed over to the client's browser, which accordingly reduces the pressure on the server.
A screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/ js/2015/js-keyword-show-red-color-codes/
The specific code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS查询替换关键字</title> <style type="text/css"> span{color:#f00;font-weight:bold;} </style> </head> <body> <p>我是标题:这里是文字介绍!</p> <p>我是老二:老二的描述性文字。</p> <script type="text/javascript"> var ps = document.getElementsByTagName('p'); for(i=0;i<ps.length;i++){ var text = ps[i].innerHTML; var index = text.indexOf(':'); var span = document.createElement('span'); span.innerHTML = text.substring(0,index+1); ps[i].innerHTML = text.substring(index+1); ps[i].insertBefore(span,ps[i].firstChild); } </script> </body> </html>
The above is the entire content of this chapter. For more related tutorials, please Visit JavaScript Video Tutorial!