Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 4.75 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I make specific words turn different colours?
  2. <div class="scroll">
  3.           <p class="margin">This box should scroll when it runs out of space.This box should                    scroll when it      runs out of space.This box should scroll when it runs                   out of space.This box should scroll when it runs        out of                  space.This box should scroll when it runs out of space.This box should scroll                   when it runs out        of space.This box should scroll when it runs                    out of space.This box should scroll when it runs out of                 space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.This box should scroll when it runs out of         space.This box                  should scroll when it runs out of space.This box should scroll when it runs out                 of      space.This box should scroll when it runs out of space.This box                 should scroll when it runs out of       space.This box should scroll                    when it runs out of space.This box should scroll when it runs out of                    space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.This box should scroll when it runs out of         space.This box                  should scroll when it runs out of space.This box should scroll when it runs out                 of      space.This box should scroll when it runs out of space.This box                 should scroll when it runs out of       space.This box should scroll                    when it runs out of space.This box should scroll when it runs out of                    space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.This box should scroll when it runs out of         space.This box                  should scroll when it runs out of space.This box should scroll when it runs out                 of      space.This box should scroll when it runs out of space.This box                 should scroll when it runs out of       space.This box should scroll                    when it runs out of space.This box should scroll when it runs out of                    space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.This box should scroll when it runs out of         space.This box                  should scroll when it runs out of space.This box should scroll when it runs out                 of      space.This box should scroll when it runs out of space.This box                 should scroll when it runs out of       space.This box should scroll                    when it runs out of space.This box should scroll when it runs out of                    space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.This box should scroll when it runs out of         space.This box                  should scroll when it runs out of space.This box should scroll when it runs out                 of      space.This box should scroll when it runs out of space.This box                 should scroll when it runs out of       space.This box should scroll                    when it runs out of space.This box should scroll when it runs out of                    space.This box should scroll when it runs out of space.This box should scroll                   when it runs out of         space.This box should scroll when it runs out                   of space.
  4.         </p>
  5.     </div>
  6.        
  7. $text = str_replace("red", "<span class='red-class'>red<span>", "This is red");
  8.        
  9. var words_and_colors = [
  10.     {'word':'this', 'color':'red'},
  11.     {'word':'to', 'color':'blue'}
  12. ];
  13.  
  14. function hilte_words_in_element (element, words) {
  15.     for (var z=0; z < words.length; z++) {
  16.         var re = new RegExp('('+words[z]['word']+'\b)', 'gi')
  17.         element.innerHTML =  element.innerHTML.replace(
  18.             re,
  19.             "<span style="color:"+words[z]['color']+"">$1</span>"
  20.         );
  21.     }
  22. }
  23. hilte_words_in_element(document.getElementById('changeMe'), words_and_colors);
  24.        
  25. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
  26.        
  27. (function($) {
  28.   var thePage = $("body");
  29.   thePage.html(thePage.html().replace(/This/ig,'<span id="This">This</span>'));
  30. })(jQuery)