我使用jquery来选择一段副本:
str = '.main-content p:nth-child('+num+')';
string = jQuery(str).html();
(num在其他地方宣布,不是一个问题)。
这将选择p标记中的所有内容(很明显),但是我选择的p标记嵌套了a标记和强标记。是否有选择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>
发布于 2016-11-21 22:51:00
您可以使用正则表达式:
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>
https://stackoverflow.com/questions/40730556
复制相似问题