javascript目标文本但不是子元素[关闭]

Posted

技术标签:

【中文标题】javascript目标文本但不是子元素[关闭]【英文标题】:javascript target text but not child element [closed] 【发布时间】:2020-12-22 14:11:11 【问题描述】:

我有以下 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; 

【问题讨论】:

您的代码有效jsfiddle.net/4f76tjp5 【参考方案1】:

您可以使用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>

【讨论】:

以上是关于javascript目标文本但不是子元素[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

jQuery实现点击空白关闭目标元素的方法

如何在 jQuery/javascript 中获取文本本身顶部的位置,而不是文本元素

javaScript绑定事件委托 demo

Javascript 或 Jquery 检查是不是将鼠标悬停在元素上

jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单

点击空白处 关闭遮罩