我有下表:

  <table id="cart-subtotals" cellspacing="0" class="shop_table
  shop_table_responsive">
  <tbody>
  <tr class="cart-subtotal">
  <th>Subtotal</th>
  <td data-title="Subtotal"><span class="woocommerce-Price-amount amount">
  <span class="woocommerce-Price-currencySymbol">£</span>165</span></td>
  </tr>
  <tr class="shipping">
  <th>Shipping</th>
  <td data-title="Shipping"> Standard Delivery: <span class="woocommerce-
   Price-amount amount"><span class="woocommerce-Price-
   currencySymbol">£</span>9</span> <input type="hidden"
   name="shipping_method[0]" data-index="0" id="shipping_method_0"
  value="flat_rate:2" class="shipping_method"></td>
  </tr>
  <tr class="order-total">
  <th>Total</th>
  <td data-title="Total"><strong><span class="woocommerce-Price-amount
  amount"><span class="woocommerce-Price-currencySymbol">£</span>174</span>
  </strong> <small class="includes_tax">(includes <span class="woocommerce-
  Price-amount amount"><span class="woocommerce-Price-
  currencySymbol">£</span>7.86</span> Reduced Rate, <span
  class="woocommerce-Price-amount amount"><span class="woocommerce-Price-
  currencySymbol">£</span>1.50</span> VAT)</small></td>
  </tr>
  </tbody>
  </table>


我需要从表的第一个中提取“ 165”的值(并排除货币符号)。

我尝试了以下操作,但不起作用:

function() {
  var element = document.querySelector('#cart-subtotal td:first');
  var price = element.innerHTML.match(/\d*\.\d*/)[0];

  return price;
}


有任何想法吗?

最佳答案

我将直接使用小计数据属性:

const subtotal = document.querySelector('td[data-title="Subtotal"]');
const price - subtotal.textContent.match(/\d+/)[0]; // 165


DEMO

10-06 12:12