Sum using jQuery each function without global variable

后端 未结 5 745
执念已碎
执念已碎 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:37

    Don't want a global?

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

提交回复
热议问题