jquery - 使用 jquery 计算总和

标签 jquery css

我有一个表,其列名是“价格”。如何计算所有产品的所有价格总和并将其存储在 jquery

中的变量中
<table id ="tab">
    <th>Product</th><th>Price</th>
    <tr><td>A</td><td>752</td></tr>
    <tr><td>B</td><td>652</td></tr>
    <tr><td>C</td><td>1000</td></tr>
</table>

最佳答案

这个怎么样?

$(document).ready(function() {
  var sum = 0;
  $('#tab tr td:nth-of-type(2)').each(function(i, cell) {
    sum += parseInt($(cell).text(),10);
  });
  console.log(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<table id="tab">
  <th>Product</th>
  <th>Price</th>
  <tr>
    <td>A</td>
    <td>752</td>
  </tr>
  <tr>
    <td>B</td>
    <td>652</td>
  </tr>
  <tr>
    <td>C</td>
    <td>1000</td>
  </tr>
</table>

关于jquery - 使用 jquery 计算总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341910/

相关文章:

javascript - 雅虎在网络邮件中的新模糊效果 - 如何?

html - 在定位时考虑溢出内容的大小

jquery - $.ajax 中的数据类型 : 'json' vs data: JSON. stringify(object)

javascript - jquery 菜单使用 slidetoggle() 小问题

jQuery 提示输入密码不想显示

html - 如何使wordpress div与页面顶部对齐?

javascript - 从 Angular Controller 有条件地加载 css

javascript - Ember.js 观察者在异步更新后不触发

javascript - 对同一函数使用不同的对象属性

javascript - Jasmine 无法检测复选框