Sum using jQuery each function without global variable

后端 未结 5 786
执念已碎
执念已碎 2021-01-14 03:47

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

So, the code will be like this with jQuery.

$(\".force\").each(function (){
    a +=          


        
5条回答
  •  灰色年华
    2021-01-14 04:16

    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;
    });
    

提交回复
热议问题