英文:
how to get b tag text from inner tags using jQuery
问题
我对jQuery逻辑还很陌生,我想从内部标签中获取`b`标签的文本,下面是我的`html`逻辑
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p><span>OI Chg : </span>
<b>51150</b>
</p>
</div>
在上面的代码中,我应该只获取b
标签的文本
例如:268400,51150
为此,我尝试了下面的逻辑,但没有得到任何值
jQuery逻辑
var d = i.find(".strikeinfocontent").closest("p >span >b").html();
var t = i.find(".strikeinfocontent").closest("p >span >b").next("p >span >b").html();
请建议我如何获取b
标签的文本?
<details>
<summary>英文:</summary>
I'm new to jQuery logics and I want to get `b` tag text from inner tags so below is my `html` logic
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p><span>OI Chg : </span>
<b>51150</b>
</p>
</div>
In the above code I should get `b` tags text only
ex: 268400 , 51150
For this I tried below logic but I didn't get any value
jQuery logic
var d = i.find(".strikeinfocontent").closest("p >span >b").html();
var t = i.find(".strikeinfocontent").closest("p >span >b").next("p >span >b").html();
Please suggest me how to get the `b` tag text?
</details>
# 答案1
**得分**: 2
选取所有带有此选择器 **".strikeinfocontent p b"** 的 b 标签,然后使用 **each** 函数对它们进行循环遍历。
```javascript
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$( ".strikeinfocontent p b" ).each(function(index) {
console.log($(this).text());
});
<!-- language: lang-html -->
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p>
<span>OI Chg : </span>
<b>51150</b>
</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<!-- end snippet -->
如果你想将这些文本保存到数组中,可以使用以下代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let arr = [];
$( ".strikeinfocontent p b" ).each(function(index) {
arr.push($(this).text());
});
console.log(arr);
let var_1 = arr[0];
let var_2 = arr[1]; // 等等...
console.log(var_1, var_2);
<!-- language: lang-html -->
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p>
<span>OI Chg : </span>
<b>51150</b>
</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<!-- end snippet -->
英文:
select all b tags with this selector ".strikeinfocontent p b" then loop through them using each function
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$( ".strikeinfocontent p b" ).each(function(index) {
console.log($(this).text());
});
<!-- language: lang-html -->
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p>
<span>OI Chg : </span>
<b>51150</b>
</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<!-- end snippet -->
you can push the texts into an array if you want to save each value in a variable you can do this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let arr = [];
$( ".strikeinfocontent p b" ).each(function(index) {
arr.push($(this).text());
});
console.log(arr);
let var_1 = arr[0];
let var_2 = arr[1]; // etc..
console.log(var_1, var_2);
<!-- language: lang-html -->
<div class=strikeinfocontent>
<p>
<span>OI : </span>
<b>268400</b>
</p>
<p>
<span>OI Chg : </span>
<b>51150</b>
</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论