简体   繁体   English

如何在javascript中访问跨度内的特定文本

[英]How to access a specific text inside a span in javascript

<div id = "item_container">

<div class="price">
            <span class ="discounted">
                   "
                   $150
                   " 
                   </span> 
                    "$125 for 2 months" 
                   <br>
                "
                Handling Fee: $3.15
                "
    </div>
</div>

Can someone help me or point me to the right direction on how to access the text "$125 for 2 months" using javascript?有人可以帮助我或为我指出如何使用 javascript 访问文本“$125 for 2 Month”的正确方向吗?

I tried using querySelector but it will only get the $150 text.我尝试使用 querySelector 但它只会得到 150 美元的文本。

var discounted_price = document.querySelector('#item_container > div.price > span').innerHTML;
console.log(discounted_price);

The code I used will not get the remaining text which i need.我使用的代码不会得到我需要的剩余文本。

If the HTML is always like that, you can query .discounted then transverse to the next sibling .如果 HTML 总是那样,你可以查询.discounted然后横向到下一个兄弟

Once you get there, you can get thenodeValue for that text node到达那里后,您可以获得该文本节点的nodeValue

 const discounted_price = document.querySelector('.discounted') console.log(discounted_price.nextSibling.nodeValue);
 <div id="item_container"> <div class="price"> <span class="discounted"> " $150 " </span> "$125 for 2 months" <br> " Handling Fee: $3.15 " </div> </div>

The text you're looking for is not inside the span , instead it's the next sibling node of the span您要查找的文本不在span ,而是在span的下一个兄弟节点

 const text = document.querySelector(".discounted").nextSibling.textContent.trim(); console.log(text)
 <html> <div id = "item_container"> <div class="price"> <span class ="discounted"> " $150 " </span> "$125 for 2 months" <br> " Handling Fee: $3.15 " </div> </div> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在某个特定类的所有实例中使用跨度来访问文本? - How to access text in all instances of a certainclass with a span inside of it? 如何为Watir WebDriver执行JavaScript以更改Span ID中的文本 - How to execute javascript for watir webdriver to change text inside a span id 如何使用纯 Javascript 从 Div 内的 Span 获取文本值 - How to get Text Value from Span inside Div with pure Javascript 如何使用javascript获取跨度内的内部文本? - How to get the inner Text inside span using javascript? 使用jQuery获取跨度内的特定文本 - Get the specific text inside span using jQuery Javascript 获取<span>元素</span>内的文本 - Javascript get text inside a <span> element 如何使用 Javascript 更改跨度文本? - How to change Span text with Javascript? 如何使用 JQuery 在单击对象内检索具有特定类的 span 元素内的文本? - How can I retrieve the text inside a span element having a specific class that is inside a clicked object using JQuery? 如何<span>在使用 JavaScript 的链接的 HTMLCollection 内的文本中<a>使用 select 文本?</a></span> - How do I select text inside a <span> that is inside an HTMLCollection of links <a> using JavaScript? 如何在特定索引处的 div 内的文本中添加 div 或跨度子字符串? - How to add div or span around substrings in text inside a div at specific indices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM