我有以下代码:
<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);
});
这段代码适用于我需要它做的事情,但是有没有办法维护斜体元素。在它被删除的那一刻,这不是一个糟糕的解决方案,但它并不完美。