【发布时间】:2023-03-21 21:59:01
【问题描述】:
我的实际查询是,我想使用 css 选择器或 xpath 检索 HTML 内部文本值。我可以通过使用 document.getElementById 但不使用选择器来实现这一点,相反,我只能打印标签元素,但不能打印其中的文本。
例如:
<li class="active">
<span id="lastPrice">1,603.35</span>
<span id="CAToday"></span><br>
<span class="up" id="change">28.80</span>
</li>
从上面的 HTML 中,我想打印 1,603.35 使用任一
Css 或
xpath
注意:我已经钻过论坛但无法找到所需的解决方案。
【问题讨论】:
-
喜欢这个?
document.querySelector('#lastPrice').textContent -
你是对的,我可以使用文档对象,但我只想使用 css / xpath 阅读,拜托。也许,像 '$$' 或 '$x'
-
我想我不明白你所说的
using css selector是什么意思,因为我的评论使用了选择器...我得到了 xpath 部分但没有 css 部分,你能解释一下吗? -
您能否分享您尝试过的代码,以便我们了解更多上下文?
-
我在 chrome 控制台上试过: $x("//*[@class='active']//span[@id='lastPrice']") 和 $x("/ /*[@class='active']//span[@id='lastPrice']/text()"),然后是 $x("//*[@id='lastPrice']")
标签: javascript html css xpath css-selectors