Use JavaScript HTML Dom to highlight specific words on the page by shawl. Qiu

Source: Internet
Author: User

Use JavaScript HTML Dom to highlight specific words on the page by shawl. Qiu

Note:
This is mainly used in the search results to highlight related words.

This function was mainly written a few days ago in the csdn community JS version of a netizen had such a demand, when the grass wrote a simple function to highlight the page specific words.

However, it was found that this function was very good, so I just wanted to improve it later.
The final result is okay. The only drawback is that the same word may have multiple colors when highlighted.
In fact, it is not difficult to show only one color for a word, but it requires more code to judge, so this function is not perfect.

Description of parameters of function fhl (O, flag, rndcolor, URL:

    Linenum

  1. /*----------------------------------------*/
  2. * Use JavaScript HTML Dom to highlight a specific word on the page by shawl. Qiu
  3. * Parameter description:
  4. * O: object to be highlighted.
  5. * Flag: a string of words or words to be highlighted. Multiple words are separated by vertical bars (|.
  6. * Rndcolor: Boolean value. indicates whether the background color and text color of the text are displayed randomly. True indicates random display.
  7. * URL: URI, whether to add links to highlighted words.
  8. /*----------------------------------------*/

Shawl. Qiu
2006-11-12
Http://blog.csdn.net/btbtd

Function fhl (O, flag, rndcolor, URL) source code and usage Demonstration:

    Linenum

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  3. <! -- Dw6 -->
  4. <Head>
  5. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  6. <Title> shawl. Qiu template </title>
  7. <SCRIPT type = "text/JavaScript">
  8. // <! [CDATA [
  9. Onload = function (){
  10. Fhl (document. Body, 'paper umbrella | her ');
  11. Fhl (document. Body, 'clove | yuxiang', true );
  12. Fhl (document. Body, 'hope | grievance ', false ,'/');
  13. Fhl (document. getelementbyid ('at _ main'), 'alone | drifting | long', true ,'/');
  14. /* Fhl (document. Body, 'paper umbrella ');
  15. Fhl (document. Body, 'hers ', false ,'/');
  16. Fhl (document. Body, 'clove ', true ,'/');
  17. Fhl (document. Body, 'yuxiang', true ,'/');
  18. Fhl (document. Body, 'expect', false );
  19. Fhl (document. Body, 'dnuse', true );*/
  20. /* Fhl (document. Body, 'clove ', 'Blue', 'White ','/');
  21. Fhl (document. Body, 'rain Lane ', 'Gray', 'White ','/');
  22. Fhl (document. Body, 'id', 'white', 'red ');
  23. Fhl (document. Body, 'length', 'white', 'red ');
  24. Fhl (document. Body, 'floppy ');*/
  25. }
  26. /*----------------------------------------*/
  27. * Use JavaScript HTML Dom to highlight a specific word on the page by shawl. Qiu
  28. * Parameter description:
  29. * O: object to be highlighted.
  30. * Flag: a string of words or words to be highlighted. Multiple words are separated by vertical bars (|.
  31. * Rndcolor: Boolean value. indicates whether the background color and text color of the text are displayed randomly. True indicates random display.
  32. * URL: URI, whether to add links to highlighted words.
  33. /*----------------------------------------*/
  34. // -------- Begin function fhl (O, flag, rndcolor, URL )------------------//
  35. Function fhl (O, flag, rndcolor, URL ){
  36. VaR bgcor = fgcor = '';
  37. If (rndcolor ){
  38. Bgcor = frndcor (10, 20 );
  39. Fgcor = fig (230,255 );
  40. } Else {
  41. Bgcor = 'yellow ';
  42. Fgcor = 'black ';
  43. }
  44. VaR Re = new Regexp (flag, 'I ');
  45. For (VAR I = 0; I <O. childnodes. length; I ++ ){
  46. VaR o _ = O. childnodes [I];
  47. VaR o_p = O _. parentnode;
  48. If (O _. nodetype = 1 ){
  49. Fhl (O _, flag, rndcolor, URL );
  50. } Else if (O _. nodetype = 3 ){
  51. If (! (O_p.nodename = 'A ')){
  52. If (O _. Data. Search (re) =-1) continue;
  53. VaR temp = felea (O _. Data, flag );
  54. O_p.replacechild (temp, O _);
  55. }
  56. } // Shawl. Qiu script
  57. }
  58. //------------------------------------------------
  59. Function felea (text, flag ){
  60. VaR style = 'style = "background-color: '+ bgcor +'; color: '+ fgcor + ';"'
  61. VaR o = Document. createelement ('span ');
  62. VaR STR = '';
  63. VaR Re = new Regexp ('+ flag +') ', 'gi ');
  64. If (URL ){
  65. STR = text. Replace (Re, '<a href = "' + URL +
  66. '"' + Style + '> $1 </a> ');
  67. } Else {
  68. STR = text. Replace (Re, '<span' + style + '> $1 </span> ');
  69. }
  70. O. innerhtml = STR;
  71. Return O;
  72. } // Shawl. Qiu script
  73. //------------------------------------------------
  74. Function frndcor (under, over ){
  75. If (arguments. Length = 1 ){
  76. VaR over = under;
  77. Under = 0;
  78. } Else if (arguments. Length = 0 ){
  79. VaR under = 0;
  80. VaR over = 255;
  81. }
  82. VaR r = frandomby (under, over). tostring (16 );
  83. R = padnum (R, R, 2 );
  84. VaR G = frandomby (under, over). tostring (16 );
  85. G = padnum (G, G, 2 );
  86. VaR B = frandomby (under, over). tostring (16 );
  87. B = padnum (B, B, 2 );
  88. // Defaultstatus = R + ''+ G +'' + B
  89. Return '#' + R + G + B;
  90. Function frandomby (under, over ){
  91. Switch (arguments. Length ){
  92. Case 1: Return parseint (math. Random () * under + 1 );
  93. Case 2: Return parseint (math. Random () * (over-under + 1) + under );
  94. Default: Return 0;
  95. }
  96. } // Shawl. Qiu script
  97. Function padnum (STR, num, Len ){
  98. VaR temp =''
  99. For (VAR I = 0; I <Len; temp + = num, I ++ );
  100. Return temp = (temp + = Str). substr (temp. Length-len );
  101. } // Shawl. Qiu script
  102. }
  103. } // Shawl. Qiu script
  104. // -------- End function fhl (O, flag, rndcolor, URL )--------------------//
  105. //]>
  106. </SCRIPT>
  107. </Head>
  108. <Body>
  109. <Div class = "at_main" id = "at_main"> <p/> <B> cite: </B> <cite> <Div class = u_cite> Dai Wangshu writes a girl <br/>
  110. <Br/>
  111. & Nbsp; Yuxiang & nbsp; <br/>
  112. Holding a paper umbrella, independent & nbsp; <br/>
  113. Long and long <br/>
  114. Youmin Yu Xiang, & nbsp; <br/>
  115. I hope to meet & nbsp; <br/>
  116. A clove is the same & nbsp; <br/>
  117. Sad girl. & Nbsp; <br/>
  118. She has & nbsp; <br/>
  119. The same color as clove, & nbsp; <br/>
  120. The same fragrance as clove, & nbsp; <br/>
  121. Sorrow like clove, & nbsp; <br/>
  122. Grief in the rain, & nbsp; <br/>
  123. Sorrow and sorrow; & nbsp; <br/>
  124. She is wandering in the lonely Rain Lane, & nbsp; <br/>
  125. Carrying a Paper Umbrella & nbsp; <br/>
  126. Like me, & nbsp; <br/>
  127. Like Me & nbsp; <br/>
  128. Keep running & nbsp; <br/>
  129. Indifference, clear, and melancholy. & Nbsp; <br/>
  130. She approached silently, & nbsp; <br/>
  131. Approaching, and output & nbsp; <br/>
  132. Sighing eyes & nbsp; <br/>
  133. She floated over & nbsp; <br/>
  134. Like a dream, & nbsp; <br/>
  135. It is as miserable and confused as a dream. & Nbsp; <br/>
  136. Wandering like a dream & nbsp; <br/>
  137. A clove location, & nbsp; <br/>
  138. This girl floated beside me; & nbsp; <br/>
  139. She was far away, far away, & nbsp; <br/>
  140. Go to the firewall of zookeeper, & nbsp; <br/>
  141. Go through the Rain Lane. & Nbsp; <br/>
  142. & Nbsp; <br/>
  143. Her color is removed, & nbsp; <br/>
  144. Dissembling the fragrance of <a href = "/"> her </a>, & nbsp; <br/>
  145. Even her & nbsp; <br/>
  146. Sighing eyes & nbsp; <br/>
  147. Clove-like melancholy. & Nbsp; <br/>
  148. Holding a paper umbrella, independent & nbsp; <br/>
  149. Long and long <br/>
  150. Youmin Yu Xiang, & nbsp; <br/>
  151. I want to float & nbsp; <br/>
  152. A clove is the same & nbsp; <br/>
  153. Sad girl. </Div> </CITE> </div>
  154. <SPAN class = "left160px"> <a href = "article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12830 # anchor "> Dai Wangshu writes girls </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12819 # anchor "> miserable world-Part 4: the daughter and daughter of blumei Street and the blood of the hero of sunny street-the fifth volume does not end with a heart below the start-four stones </a> <br/> <a href = "article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12835 # anchor "> qingyu case & nbsp; yuanxi </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12855 # anchor ">" Scientific Spirit "Semantic Analysis </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 3053 # anchor "> goodbye to Kangqiao -- Xu Zhimo </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12862 # anchor "> academic paper format </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12836 # anchor "> a blooming tree </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12840 # anchor "> letter writing format </a> <br/> <a href =" article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12818 # anchor "> miserable world-Part 4: the daughter of blumei Street and the blood of the hero of San duni street-12th vol. Collins-6 waiting </> <br/> <a href = "article. asp? Classid = 14 & nclassid = 178 & ArticleID = 12834 # anchor "> yunge </a> </span>
  155. </Body>
  156. </Html>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.