Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript进行计数_Javascript_Jquery - Fatal编程技术网

使用Javascript进行计数

使用Javascript进行计数,javascript,jquery,Javascript,Jquery,它显示totalcount=285,而不是15计数 还尝试了parseFloat$'.share count'[0]。innerHTML,但给出了一个错误 未捕获的TypeError:无法读取未定义的属性“innerHTML” 是否有任何方法可以通过使用for循环或任何其他方法获得总计数?请帮助我 var totalcount =$('.share-count')[0].innerHTML + $('.share-count')[1].innerHTML + $('.share-count')[

它显示totalcount=285,而不是15计数

还尝试了parseFloat$'.share count'[0]。innerHTML,但给出了一个错误

未捕获的TypeError:无法读取未定义的属性“innerHTML”

是否有任何方法可以通过使用for循环或任何其他方法获得总计数?请帮助我

var totalcount =$('.share-count')[0].innerHTML + $('.share-count')[1].innerHTML + $('.share-count')[2].innerHTML;
或通用:

var totalcount = parseInt($('.share-count:eq(0)').text()) + 
parseInt($('.share-count:eq(1)').text()) +
parseInt($('.share-count:eq(2)').text())

JavaScript放在HTML中的什么位置?在HTML之前还是之后?你能尝试用.innerText替换.innerHTML,然后用parseInt代替parseFloat吗?@DavidLi it stll会给我错误Uncaught TypeError:无法读取Undefinedoh的属性“innerText”我不知道你在使用jquery对象。html和文本的jquery对象的函数是html和文本,而不是innerHTML和innerText。请尝试$'.share count'[0]。textTested var totalcount=parseInt$'.share count'[0]。innerHTML+parseInt$'.share count'[1]。innerHTML+parseInt$'.share count'[2]。innerHTML;而且效果很好。
var totalcount = parseInt($('.share-count:eq(0)').text()) + 
parseInt($('.share-count:eq(1)').text()) +
parseInt($('.share-count:eq(2)').text())
var totalcount = $('.share-count')
  .map(function(i,e){ // convert array of elements to array of ints
    return parseInt($(e).text());
  }) 
  .toArray()
  .reduce(function(previousValue, currentValue) { // calculate sum of array
    return previousValue + currentValue
  }, 0)