计算每个段落中的字符数 [英] Count number of characters in each paragraph

查看:36
本文介绍了计算每个段落中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来计算页面上每个段落中的字符数.我在下面发现了这个小片段,它计算每个段落中的单词数,效果很好.是否可以修改为也包括字符数.互联网上有很多解决方案,但它们只关注特定的字符串或文本区域,并且往往变得非常冗长和复杂.我不介意空格或特殊字符是否包含在计数中.

i am trying to find a way to count the number of characters in every paragraph on a page. I discovered this little snippet below which counts the number of words in each paragraph and it works great. Could it be modified to also include the number of characters as well. There are a bunch of solutions all over the internet but they only focus on a specific string or text area and tend to get really long and intricate. I dont mind if the spaces or special characters are included in the count.

这是片段.这是一段非常简洁的小代码.

Here is the snippet. It is a really neat little piece of code.

$(document).ready(function() {
    $('p').each(function(i) {
        var iTotalWords = $(this).text().split(' ').length;
        $(this).append("<b> " + iTotalWords + " words </b>");
    });
})

提前致谢.

推荐答案

这里是解决方案:

var count = $('p').text().length;

所以你的代码会是这样的:

So your code will be like:

$(document).ready(function() {
    $('p').each(function(i) {
        var iTotalWords = $(this).text().split(' ').length;
        var charCount = $(this).text().length;
        $(this).append("<b> " + iTotalWords + " words and " + charCount + " chars </b>");
    });
})

希望对你有所帮助;D

这篇关于计算每个段落中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆