为.each增加价值

时间:2011-12-18 14:08:00

标签: javascript jquery html

以下HTML

<div class="commentlinks">
    <a class="commentlinked blogcount">3</a>
    <a class="commentlinked fbcount">2</a>
    <a class="commentlinked disqcount">1</a>
    <a class="allcommentcount"></a>
</div>

使用此jQuery

$('.commentlinks').each(function() {
var currentValue = parseInt($(".blogcount").text());
var currentValue2 = parseInt($(".fbcount").text());
var currentValue3 = parseInt($(".disqcount").text());
var newValue = currentValue + currentValue2 + currentValue3;
$(".allcommentcount").text(newValue);
});

成功返回http://jsfiddle.net/hQzZQ/22/

    3     2     1     6

但是当我有这个HTML

<div class="commentlinks">
    <a class="commentlinked blogcount">3</a>
    <a class="commentlinked fbcount">2</a>
    <a class="commentlinked disqcount">1</a>
    <a class="allcommentcount"></a>
</div>

<div class="commentlinks">
    <a class="commentlinked blogcount">7</a>
    <a class="commentlinked fbcount">6</a>
    <a class="commentlinked disqcount">1</a>
    <a class="allcommentcount"></a>
</div>

它返回http://jsfiddle.net/hQzZQ/23/

3 2 1 74 7 6 1 74

为什么它返回它不正确帮我解决它!

1 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/hQzZQ/24/

$('.commentlinks').each(function() {
var currentValue = parseInt($(".blogcount",this).text());
var currentValue2 = parseInt($(".fbcount",this).text());
var currentValue3 = parseInt($(".disqcount",this).text());
var newValue = currentValue + currentValue2 + currentValue3;
$(".allcommentcount",this).text(newValue);
});