gpt4 book ai didi

javascript - jQuery:获取可能包含或不包含链接的段落标记的文本内容

转载 作者:行者123 更新时间:2023-12-02 18:56:39 24 4
gpt4 key购买 nike

我使用以下方法成功从 div 集合中的第一段获取文本:

$('#businessListings div.listing').each(function() {
console.log('text: ' + $(this).find('p:first').html());
});

示例 div 如下所示:

<div id="businessListings" class="unifyRepeatArea">
<div class="unifyRepeat listing">
<p>123 ABC Child Care</p>
<p>Jane Doe, Owner</p>
<p>555-555-1234</p>
<img src="images/123ABCchildcare.jpg" width="50" height="50">
</div>
...
</div>

问题是,集合中的某些段落可能有一个链接:

<p><a href="http://www.123abcchildcare.com">123 ABC Child Care</a></p>

如何获取每个 div 第一个段落中的文本,无论该段落是否有链接?

Working jsFiddle .

最佳答案

您应该尝试使用 text() 而不是 html():

$('#businessListings div.listing').each(function() {
console.log('text: ' + $(this).find('p:first').text());
});

这是一个jsFiddle .

关于javascript - jQuery:获取可能包含或不包含链接的段落标记的文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15272184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com