私は次のコードを持っています:
<table>
<th class="title2">The <i>very</i> hungry school</th><br />
<th class="title2">The very hungry school <span>yeah it works</span></th>
と..
function capitalise(str) {
if (!str) return;
var counter = 0;
var stopWords = ['a', 'an', 'and', 'at', 'but', 'by', 'far', 'from', 'if', 'into', 'of', 'off', 'on', 'or', 'so', 'the', 'to', 'up'];
str = str.replace(/\b\S*[a-z]+\S*\b/ig, function(match) {
counter++;
return $.inArray(match, stopWords) == -1 || counter === 1 ? match.substr(0, 1).toUpperCase() + match.substr(1) : match;
});
return str;
}
$('th.title2').each(function() {
var capclone = $(this).clone().children(':not(i)').remove().end();
capclone.text(capitalise(capclone.text()));
capclone.append($(this).children(':not(i)'));
$(this).replaceWith(capclone);
});
このコードは、私が必要としていることに対して機能しますが、イタリック要素を維持する方法があります。削除された時点では、それは悪い解決策ではありませんが、完璧ではありません。