I am trying to implement a function using jQuery
that will scan through my entire blog post and calculate the estimated read time for the user.
我正在尝试使用jQuery实现一个函数,该函数将扫描我的整个博客文章并计算用户估计的阅读时间。
Currently I've done the following to get the amount of words in each paragraph:
目前,我已经做了以下的事情来获得每段的字数:
$('p').each(function(){
var v = wordCount($(this).html());
totalWords = totalWords + v.words;
);
function wordCount(val){
var wom = val.match(/\S+/g);
return{
charactersNoSpaces: val.replace(/\s+/g, '').length,
characters: val.length,
words: wom ? wom.length: 0,
lines: val.split(/\r*\n/).length
}
}
Thank you to the poster on this question for the wordCount
function: Word and Character Count using jQuery
感谢关于wordCount函数的问题的海报:使用jQuery进行单词和字符计数
The above works fine and I get the output I expect for my paragraphs. However my Blog post will consist of a combination of h1
, h2
, h3
, h4
, h5
, h6
, ul
,ol
, span
,p
, li
.
上面的操作很好,我得到了我想要的段落的输出。然而,我的博客文章将包括h1、h2、h3、h4、h5、h6、ul、ol、span、p、li的组合。
So I modified my code like this:
所以我修改了我的代码
$('p, h1, h2, h3, h4, h5, h6, ul li, ol li, span').each(function(){
var v = wordCount($(this).html());
totalWords = totalWords + v.words;
});
But now the results are skewed. For example If I have a span
tag within a paragraph or a list item or whatever it counts it twice.
但现在的结果是扭曲的。例如,如果我在一个段落或一个列表项中有一个span标记,或者任何它计算它两次的东西。
So for example this Markup should return 8, instead I get 9.
例如,这个标记应该返回8,而不是9。
<ul>
<li>This is a test</li>
<li>This is <span>another</span> text</li>
</ul>
Can anyone advise me on a possible fix, there is obviously an error in my logic and I'd appreciate any help.
有人能给我建议一个可能的解决办法吗?很明显,我的逻辑有错误,我希望得到任何帮助。
Thanks
谢谢
2 个解决方案
#1
6
Use text()
to get the textContent of the element.
使用text()获取元素的textContent。
var v = wordCount($(this).text());
If html()
is used, the elements, for example, <h1>
and </h1>
will be counted as words.
如果使用html(),那么元素(例如,
和
)将被算作单词。#2
0
Try modifying
试着修改
var v = wordCount($(this).html());
var v = wordCount($(). html());
to
来
var v = $(this).clone().children().remove().end().html();
v = wordCount(v);
Source: Using .text() to retrieve only text not nested in child tags
源:使用.text()只检索不嵌套在子标记中的文本
#1
6
Use text()
to get the textContent of the element.
使用text()获取元素的textContent。
var v = wordCount($(this).text());
If html()
is used, the elements, for example, <h1>
and </h1>
will be counted as words.
如果使用html(),那么元素(例如,
和
)将被算作单词。#2
0
Try modifying
试着修改
var v = wordCount($(this).html());
var v = wordCount($(). html());
to
来
var v = $(this).clone().children().remove().end().html();
v = wordCount(v);
Source: Using .text() to retrieve only text not nested in child tags
源:使用.text()只检索不嵌套在子标记中的文本