在没有全局变量的情况下使用 jQuery 对每个函数求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4377410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:10:25  来源:igfitidea点击:

Sum using jQuery each function without global variable

jqueryeach

提问by Jinbom Heo

I want to add some HTML elements that have the same class name.

我想添加一些具有相同类名的 HTML 元素。

So, the code will be like this with jQuery.

因此,代码将与 jQuery 类似。

$(".force").each(function (){
    a += parseInt( $(this).html());
});
$("#total_forces").html(a);

In this code, the variable has to be global.

在这段代码中,变量必须是全局的。

Is there any other elegant way to sum every .forcevalue and get the sum out of the eachfunction without global variable?

有没有其他优雅的方法来对每个.force值求和并在each没有全局变量的情况下从函数中求和?

回答by Spiny Norman

If you don't want to introduce a global variable, you could use something like this:

如果你不想引入一个全局变量,你可以使用这样的东西:

$("#total_forces").html(function() {
    var a = 0;
    $(".force").each(function() {
        a += parseInt($(this).html());
    });
    return a;
});

回答by user113716

For convenience, if you're going to be needing this same functionality frequently, I'd probably just make a plugin.

为方便起见,如果您经常需要相同的功能,我可能会制作一个插件。

Example:https://jsfiddle.net/tzw4mkL2/

示例:https : //jsfiddle.net/tzw4mkL2/

(function( $ ) {
    $.fn.sumHTML = function() {
       var sum = 0;
        this.each(function() {
           var num = parseInt( $(this).html(), 10 );
           sum += (num || 0);
        });
       return sum; 
    };
})( jQuery );

...which would be used like this:

...将像这样使用:

$('#total_forces').html( $('.force').sumHTML() );


EDIT:Changed to guard agains NaNas noted by @?ime Vidas. Also ensured base-10 in the parseInt()and fixed a closing }that was missing.

编辑:NaN@?ime Vidas所指出的,改为后卫。还确保了 base-10parseInt()并修复了}丢失的关闭。

回答by Matt

In short, no.

简而言之,没有。

Why does ahave to be global? It doesn't haveto be global.

为什么a必须是全球性的?它不具备成为全球性的。

function aFunc() {
    var a = 0;

    $(".force").each(function (){
        a += parseInt( $(this).html());
    });

    return a;
}

$("#total_forces").html(aFunc());

Which, can be simplified to:

其中,可以简化为:

$("#total_forces").html(function() {
    var a = 0;

    $(".force").each(function (){
        a += parseInt( $(this).html());
    });

    return a;
});

Here ais local to aFunc, and is just one of millions of examples of it not being in the global scope.

这里a是本地的aFunc,只是它不在全局范围内的数百万个例子之一。

回答by jensgram

Don't want a global?

不想要一个全球?

(function() {
    var a = 0;
    $('.force').each(function (){
        a += parseInt($(this).text());
    });
    $('#total_forces').text(a);
})();

回答by linkyndy

You can use $(".force").length, it returns the number of elements in the jQuery object.

您可以使用 $(".force").length,它返回 jQuery 对象中元素的数量。

jQuery API

jQuery API