javascript - 当 Div 名称不唯一时,使用 jQuery 从 Div 中获取文本
问题描述
有没有办法根据它的位置来获取 div 的文本内容?
<div class="locationDetailsRight">
<div class="shiptoexpanded">
<div>3 / 15</div>
<div group-key="Shipping 12/2/2020">
<div class="shipSort">20201202</div>
12/2/2020
</div>
<div group-key="">$53.11</div>
<div group-key="">$159.33</div>
<div group-key="">$11.10</div>
<div group-key="">$170.43</div>
</div>
</div>
我想抢 159.33 美元和 170.43 美元。
我写的:
console.log($(".locationDetailsRight .shiptoexpanded").text())
回来:
"
3 / 15
20201202
12/2/2020
$53.11
$159.33
$11.10
$170.43
"
是否可以在我需要的行中获取文本?
解决方案
使用第 n 个子选择器https://api.jquery.com/nth-child-selector/
对于您的代码,以下将起作用
alert($(".shiptoexpanded").find(':nth-child(4)').text());
alert($(".shiptoexpanded").find(':nth-child(6)').text());
这是JSFiddle
推荐阅读
- c# - CsvHelper - 缓慢填充数据表(ish)
- html - 在引导程序中居中表单不起作用
- r - R/Shiny - 将复选框标签转移到功能字段
- autodesk - EAGLE:是否可以使用“print-inductor.ulp”文件创建 90 度轨迹转弯?
- c# - for 循环内的异步 HTTP 请求,没有同时等待异步操作
- macos - 在 macOS 上,为什么 'docker system df' 显示总共 18 个 * 和 * 活动容器,但 'docker ps' 是空的
- ruby-3 - montrose gem 在使用期间不工作
- c++ - 尝试使用带有 arduino ide 的 esp32 制作自定义快捷键
- google-chrome - 如何自定义 Chromium(Windows 平台)
- c++ - 替换 gtk_dialog_get_action_area?