このフィドルにも以下のコードがあります。
HTML
<span class="myName">John Smith</span>
<span class="myName">John Smith</span>
<span class="myName">John Smith</span>
<span class="myName">John Smith</span>
<span class="myName">John Smith</span>
<span class="myName">John Smith</span>
JavaScript
$('.myName').each(function(){
var text = $(this).html().split(' '),
len = text.length,
test = $(this).val().length,
result = [];
console.log('outside: ' + test);
for( var i = 0; i < len; i++ ) {
result[i] = '<span class="name-'+[i]+'" >' + text[i] + '</span>';
console.log('inside: ' + test);
}
$(this).html(result.join(' '));
console.log('after: ' + test);
});
myName div の各単語にスパンを追加します。各 div の文字数を数えたいので、名前が長い場合は、名前を J Smith に切り詰めることができます。
しかし、コンソール ログを見ると、3 つの異なる場所で長さをカウントしようとしましたが、常に 0 として返されます。私は何を間違っていますか?