繁体   English   中英

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

[英]jQuery: Getting text contents of paragraph tag that may or may not contain a link

我使用以下方法成功获取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的第一段中的文本,段落是否有链接?

工作jsFiddle

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

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

这是一个jsFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM