我有问题要问你们。假设我正在写一篇文章,我有 10 个关键字,它们应该在正文中提及。如果提到关键字,我需要计算这个词在文本中出现的次数。并且所有数量都应该显示在 textarea 的顶部或底部,例如跨度或输入,这无关紧要。但是怎么做?
更新:
对不起,我忘了提到我想在输入 textarea 时这样做,它需要在 jquery 中进行。
function ck_jq()
{
var charsCount = CKEDITOR.instances['article'].getData().replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '').replace(/^\s+|\s+$/g, '');
var wordCount = CKEDITOR.instances['article'].getData().replace(/[^\w ]/g, "").split(/\s+/);
var max = <?php echo $orderInfo->wordstarget; ?>;
//var max = 5;
if (wordCount >= max) {
var over = max - wordCount.length;
$("#wordstarget").css('color', 'red');
$("#characterscount").css('color', 'red');
$("#words").css('color', 'red');
$("#wordsleft").css('color', 'red');
// $("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ char);
$("#wordstarget").text(max + " words target");
$("#characterscount").text(charsCount.length + " characters");
$("#words").text(wordCount.length + " words");
$("#wordsleft").text(over +" words left");
//$("#wordscount").css('color', 'red');
//$("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ over);
} else {
var char = max - wordCount.length;
$("#wordstarget").css('color', 'green');
$("#characterscount").css('color', 'green');
$("#words").css('color', 'green');
$("#wordsleft").css('color', 'green');
// $("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ char);
$("#wordstarget").text(max + " words target");
$("#characterscount").text(charsCount.length + " characters");
$("#words").text(wordCount.length + " words");
$("#wordsleft").text(char +" words left");
}
}
我用它来计算单词和字符。CKEDITOR.instances['article'].getData()
使用它我可以获取所有 ckeditor 文本,然后搜索确切的单词。