1.9 wrap in spans
by octatone
HTML
<h2>1.9.0</h2>
<div class="result"/>
JavaScript
function wrapCharsInSpan (text) {
var $node = $('<div/>').html(text);
var $this, html;
$node.children().addBack().contents().each( function (index, element) {
// only the text!
if (element.nodeType == 3) {
$this = $(element);
var text = $this.text().replace(/(.)/g, "<span>$&</span>");
$this.replaceWith(String(text));
}
});
html = $node.html();
$node.remove();
$node = null;
return html;
}
var pre = $('<pre/>').text(wrapCharsInSpan('<strong>String</strong>'));
$('.result').append(pre);