我在尝试获取一组 <p>
之间的文本时遇到问题使用 JQuery 的标签 - 这是我目前拥有的 html:
<div class="mainarea">
<p>
<strong>Location: </strong>
</p>
<!-- display search results -->
<p>
You searched for
<strong>"xyz"</strong>.
</p>
我正在尝试获取第二组 <p>
中的文本标签(您搜索的是...),但我不断从第一个 <p>
中获取文本标记(位置...)。
到目前为止我有这个功能:
$('.mainarea p')
,它返回“Location...”并尝试向其添加 .text() 函数,但这似乎没有帮助。
最佳答案
使用eq()或 :eq selector
$('.mainarea p').eq(1).text();
使用选择器:
$('.mainarea p:eq(1)').text();
Note: Used .text() as mentioned in the text. To get innerHTML use .html()
$('body').html($('.mainarea p').eq(1).html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="mainarea">
<p>
<strong>Location: </strong>
</p>
<!-- display search results -->
<p>
You searched for <strong>"xyz"</strong>.
</p>
关于javascript - JQuery 在一组特定的 p 标签中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607514/