开发者

Using jQuery, how to get sum of numbers from innerText

I would like to get the total sum of the results in Stocks, ETFs and Mutual Funds. No. of results are always shown within brackets.

<div class="tabWrapper contains">
   <a class="active" data-paramvalue="Equities" href="javascript:void(0);">Stocks (10)</a>
   <a data-paramvalue="ETFs" href="javascript:void(0);">ETFs (15)</a>
   <a href="javascript:void(0);" data-paramvalue="Mutual Funds" class="last">Mutual Funds (5)</a>
</div>

For this htm开发者_如何学Cl tag, I would like to get 30. Number 30 is derived by adding 10+15+5.


Try this:

var sum = 0;
var re = /\(([0-9]+)\)/;

$('a').each(function() {
   var match = re.exec($(this).text());
   if(match && match.length > 1) {
      sum += parseInt(match[1], 10);
   }
});


Find the elements and .each() over them using RegExp to find the count and add it to a running total:

http://jsfiddle.net/JAAulde/EbBv5/3/

(edit: added some error checking)



var sum = 0;
var regex = /\(([0-9]+)\)/g;
jQuery("a").each(function(){

var match = regex.exec($(this).text());
sum = sum + match[1];



});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜