繁体   English   中英

选择p标签,但排除一些孩子

[英]Select p tag, but exclude some children

我正在使用jquery来选择一部分副本:

str = '.main-content p:nth-child('+num+')';
string = jQuery(str).html();

(num在别处声明,不是问题)。

这会选择p标签中的所有内容(显然),但是我选择的p标签嵌套了标签和强标签。 有没有办法选择p标签并排除强标签。 我试过以下代码:

str = '.main-content p:nth-child('+num+') :not(strong)';

但是这会选择所有子元素(不包括强),但不会选择实际p标记的内容。

任何想法都会受到欢迎!

提前致谢!

编辑 - 请求的示例:

<p><strong>Content that I want to ignore</strong> This is some content which I would like to include. <a href="#">also keep this</a></p>

最好还是这样:

<p>This is some content which I would like to include. <a href="#">also keep this</a></p>

或这个:

This is some content which I would like to include. <a href="#">also keep this</a>
var p = $('.main-content p:nth-child('+num+')').clone();
p.find('*:not(a)').remove();
var your_string = p.html();

或者您可以指定要删除的确切标签:

p.find('strong, b, i').remove();

您可以使用正则表达式:

 var str = $('p').html(); str = str.replace(/<strong>[^<]*<\\/strong>/gi,''); console.log(str.trim()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><strong>Content that I want to ignore</strong> This is some content which I would like to include. <a href="#">also keep this</a></p> 

暂无
暂无

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

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