javascript 目标文本但不是子元素
javascript target text but not child element
我得到以下 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>
我得到以下 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>