Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've devised one way of doing it - the code could probably be tidied up and shrunk, but it works.</p> <p>The idea is you have two nested span tags - one for the styled text which has already been highlighted, and another to hold only the next word in the sequence for jQuery event binding.</p> <p>Put whatever string you want in the _string variable and away you go.</p> <pre><code>&lt;style type="text/css"&gt; html { font-size: 20pt; } .red { color: red; } &lt;/style&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript"&gt; var _highlightIndex = 0; var _string = "Here is some test text to highlight"; $(document).ready(function() { RenderSpan(); }); function BindEvents() { $("#outer .nextword").mouseover(function() { RenderSpan(); }); } function RenderSpan() { var array = _string.split(" "); // Use an array to store text to save repeated string concatenation var html = []; var i = 0; html[i++] = "&lt;span class=\"red\"&gt;" // Add all elements that have already been turned red for (var j = 0; j &lt; _highlightIndex; j++) { html[i++] = array[j]; html[i++] = " "; } // Close the red span html[i++] = "&lt;/span&gt;"; // Add the next word in the sequence to its own span for the event to bind to html[i++] = "&lt;span class=\"nextword\"&gt;"; html[i++] = array[_highlightIndex]; html[i++] = "&lt;/span&gt;"; html[i++] = " "; // Now add the remaining words with no span around them for (var k = _highlightIndex + 1; k &lt; array.length; k++) { html[i++] = array[k]; html[i++] = " "; } // Replace the html in the outer div $("#outer").html(html.join("")); // Increment the highlight index _highlightIndex++; // move this to the top of the function if you want to start with one red word // Re-bind the jQuery mouseover BindEvents(); } &lt;/script&gt; &lt;div id="outer"&gt;&lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload