我试图找出如何在句号之后为第一个单词着色。
我找到了第一个字母的大写字母,但现在我需要它的颜色。我更喜欢成为第一个字母" red"。
$(function () {
function capitalizeSentences(capText, capLock) {
if (capLock == 1 || capLock == true) {
capText = capText.toLowerCase();
}
capText = capText.replace(/\.\n/g, ".[-<br>-]. ");
capText = capText.replace(/\.\s\n/g, ". [-<br>-]. ");
var wordSplit = '. ';
var wordArray = capText.split(wordSplit);
var numWords = wordArray.length;
for (x = 0; x < numWords; x++) {
wordArray[x] = wordArray[x].replace(wordArray[x].charAt(0), wordArray[x].charAt(0).toUpperCase());
if (x == 0) {
capText = wordArray[x] + ". ";
} else if (x != numWords - 1) {
capText = capText + wordArray[x] + ". ";
} else if (x == numWords - 1) {
capText = capText + wordArray[x];
}
}
capText = capText.replace(/\[-<br>-\]\.\s/g, "\n");
capText = capText.replace(/\si\s/g, " I ");
return capText;
}
//Capitalize After Period
$("#capitalizeAfterPeriod").click(function () {
var txt = $('textarea#generator').val();
txt = capitalizeSentences(txt, true);
$('textarea#generator').val(txt).addClass('period');
});
});
答案 0 :(得分:1)
您无法在textarea中为一个字母添加颜色,因为textareas不支持富文本,如果要使用富文本,则需要使用具有contenteditable属性的元素,然后将该行更改为:
wordArray[x] = wordArray[x].replace(wordArray[x].charAt(0), '<span style="color:#color;">'+wordArray[x].charAt(0).toUpperCase()+'</span>');