var findText = function(element, pattern, callback) { if ( ! element.childNodes) { return; } for (var childi = element.childNodes.length; childi-- > 0;) { var child = element.childNodes[childi]; if (child.nodeType == 1) { findText(child, pattern, callback); } else if (child.nodeType == 3) { var matches = []; var match; while (match = pattern.exec(child.data)) matches.push(match); for (var i = matches.length; i-- > 0;) callback.call(window, child, matches[i]); } } } findText(document.body, /./g, function(node, match) { var element = document.createElement('span'); node.splitText(match.index + 1); element.appendChild(node.splitText(match.index)); node.parentNode.insertBefore(element, node.nextSibling); }); var spans = document.getElementsByTagName('span'), spansLength = spans.length, currentSpan = 0, interval = setInterval(function() { if (currentSpan == spansLength) { clearInterval(interval); } spans[currentSpan++].style.textDecoration = 'line-through'; }, 100);
jsFiddle .
- Go through each character (except
\n ) with regex and recursively apply a callback function to wrap each individual character with span . - Select all of these
span elements, and then use setInterval() to move through them by adding style="text-decoration: line-through through the style span object. - Stop when we iterate through each
span .
The disadvantage of using innerHTML is that when you serialize HTML, you lose all events, etc. In the above script, the strong element is still clickable (you will click the span , which will bubble for the parent).
alex
source share