私はあなたが説明したとおりに正確に行います。実際には、私は実際に行った:
<div>
The dog jumps over the log.
</div>
<script type="text/javascript">
function boxWords(textNode) {
var frag = document.createDocumentFragment(),
// trim leading and trailing whitespace to avoid empty elements
words = textNode.nodeValue.replace(/(^\s+)|(\s+$)/g, '').split(' ');
for(var i = 0, len = words.length; i < len; ++i) {
frag.appendChild(document.createElement('span').appendChild(
document.createTextNode(words[i])).parentNode);
frag.appendChild(document.createTextNode(' '));
}
textNode.parentNode.replaceChild(frag, textNode);
}
var div = document.getElementsByTagName('div')[0];
boxWords(div.firstChild);
// underline the 4th word
div.getElementsByTagName('span')[3].style.textDecoration = 'underline';
</script>
隠されていた単語が崩壊するスペースはありますか?白い空白で開いたままになっていますか? – Patrick
私はスペースを維持する必要がありますので、私の推測では、スペースはDIVに戻された各SPANで再挿入されるということです。 – Xeoncross
申し訳ありませんが、私はあなたの質問を読んだ。私は、各単語が占有するスペースが縮小するのではなく、元のままにしたいと考えています。 – Xeoncross