javascript目标文字,但不是子元素

问题描述

我有以下html:

<span class="woocommerce-order-overview__total total">
    Gesamt: <strong><span class="woocommerce-Price-amount amount">0,85<span class="woocommerce-Price-currencySymbol">€</span></span></strong>
</span>

我想要的输出是值0,85,但没有货币符号。我该如何修改此Javascript?

function(){ var capturedText = document.querySelector(".woocommerce-order-overview__total span.woocommerce-Price-amount").innerText.match(/^(.*).{1}/i)[1].trim(); return capturedText; }

解决方法

您可以使用childNodes来获取该文本节点:

var capturedText = document.querySelector(".woocommerce-order-overview__total span.woocommerce-Price-amount").childNodes[0].textContent;

console.log(capturedText);
<span class="woocommerce-order-overview__total total">
    Gesamt: <strong><span class="woocommerce-Price-amount amount">0,85<span class="woocommerce-Price-currencySymbol">€</span></span></strong>
</span>