I have a html that contains tables like the following example
-
Try something like this, using a more functional approach:
var sum = [].map.call(valuelements, function(v) {
return +v.textContent; }).reduce(function(a, b) { return a + b; });
讨论(0)
-
You need to initialize counter
with a starting number, otherwise you're performing math on undefined
.
var counter = 0;
And then in the loop, use parseInt
, parseFloat
, or a direct number conversion on the .innerHTML
value.
var counter = 0;
for (var i=0; i<features.length; i++){
counter += parseFloat(valuelements[i].innerHTML);
}
In a modern browser, you could do this:
var count = [].reduce.call(valueelements, function(c, v) {
return c + parseFloat(v.innerHTML);
}, 0);
讨论(0)
- 热议问题