如何使用jQuery从内部标签中获取``标签的文本?

huangapple go评论52阅读模式
英文:

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&#39;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&#39;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

选取所有带有此选择器 **&quot;.strikeinfocontent p b&quot;** 的 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 -->

$( &quot;.strikeinfocontent p b&quot; ).each(function(index) {
  console.log($(this).text());
});

<!-- language: lang-html -->

&lt;div class=strikeinfocontent&gt;
  &lt;p&gt;
    &lt;span&gt;OI : &lt;/span&gt;
    &lt;b&gt;268400&lt;/b&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;span&gt;OI Chg : &lt;/span&gt;
    &lt;b&gt;51150&lt;/b&gt;
  &lt;/p&gt;
&lt;/div&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.min.js&quot; integrity=&quot;sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- 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 = []; 

$( &quot;.strikeinfocontent p b&quot; ).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 -->

&lt;div class=strikeinfocontent&gt;
  &lt;p&gt;
    &lt;span&gt;OI : &lt;/span&gt;
    &lt;b&gt;268400&lt;/b&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;span&gt;OI Chg : &lt;/span&gt;
    &lt;b&gt;51150&lt;/b&gt;
  &lt;/p&gt;
&lt;/div&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.min.js&quot; integrity=&quot;sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 20:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定