English 中文(简体)
每个条目的查询单词数
原标题:jQuery word count on each entry
  • 时间:2012-05-25 15:40:09
  •  标签:
  • jquery
  • count

我在博客主题上工作, 每个文章边的每个条目都有字数数。 我可以让字数数起作用, 但是它只在第一个条目上起作用, 然后为每个文章显示相同的数字 。 我需要修改下面的脚本, 找到最近的 < code> div. entrycontent , 并计算其中的单词, 但每个条目 。 下面是我的输入标记代码, 如果有人能帮助的话, 请感谢 。

<div class="entry">
    <div class="entryinfo">
        <script type="text/javascript">
            var text = $( .entrycontent ).text();
            var wordCount = text.split(   ).length;
            $("span.words").text(wordCount +   words );
        </script>
        <span class="words"></span>
    </div>
    <div class="entrycontent">
        Lorem ipsum dolor amet...
    </div>
</div>
最佳回答

您需要使用 .each () 循环。

此脚本在页面上放置一次, 位于底部或顶部 $( document).ready( 函数( {...) } ; > 块中 :

$( .entry ).each(function(i,el) {
    var $entry = $(this),
        text = $entry.find( .entrycontent ).text(),
        wordCount = text.split(   ).length;
    $entry.find("span.words").text(wordCount +   words );
    $entry.find("span.chars").text(charCount); // IDs must be unique, use classes instead
});

<强> UPDATE

$entry. findd (.entrycontent. text () 包含许多白空格时,无论是否分隔单词,每个空格字符都会分裂。 尝试一下 :

$( .entry ).each(function(i,el) {
    var $entry = $(this),
        text = $entry.find( .entrycontent ).text(),
        wordCount = text.split(/s+/).length;
    $entry.find("span.words").text(wordCount +   words );
});

.split () docs

<强 > UPDATE 2

好吧,如果你想要一个真正的字数,我想我们应该使用.match () 而不是.spliit () :

$( .entry ).each(function(i,el) {
    var $entry = $(this),
        text = $entry.find( .entrycontent ).text(),
        marr = text.match(/w+/g) || [],  // null if no matches
        wordCount = marr.length;
    $entry.find("span.words").text(wordCount +   words );
});
问题回答
$( .entry ).each(function() {
 var text = $(".entrycontent", this).text(),
     wordCount = text.split(/s+/).length;
 $("span.words", this).text(wordCount +   words );
 $("span#chars", this).text(charCount);
})

它可能与你的选择者 被绑在任何元素上有关 任何元素有 进入内容的类别。

我建议每个条目都这样转动:

$(".entrycontent").each(function() {
    var entryInfo = $(this).prev();
    var text = $(this).text(); 
    var wordCount = text.split(   ).length;  
    entryInfo.find("span.words").text(wordCount +   words );  
});




相关问题
SELECT command to calculate percentage

I m trying to get the percentage of each video I have in my database based on its view count against all other videos. I m then trying to display all the videos from highest view count to lowest, ...

Consolidating a COUNT query

I have a page where I am running an initial SQL query to get a list of subjects, then I loop over this query and run two additional queries for each record returned from the original subjects query (I ...

R: Count number of objects in list [closed]

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) ...

Mysql get count of rows for each day

My Current query is: SELECT DISTINCT DATE(vote_timestamp) AS Date, COUNT(*) AS TotalVotes FROM `votes` WHERE vote_target_id= 83031 GROUP BY DATE(vote_timestamp) ORDER BY DATE(vote_timestamp) DESC ...

Group by named column

I always forget how to do things like this. I have a database table with birthdates and I want to find out how many people have the same age. I m trying: SELECT TIMESTAMPDIFF( YEAR, birthdate, ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

热门标签