Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择p标记,但排除一些子项_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 选择p标记,但排除一些子项

Javascript 选择p标记,但排除一些子项,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用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)'; 但这会选择所有子

我正在使用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)';
但这会选择所有子元素(不包括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>
这是我想包括的一些内容。
或者,您可以指定要删除的精确标记:

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

您可以使用正则表达式:

var str=$('p').html();

str=str.replace(/[^能否显示示例HTML和所需的输出?为要修改的元素添加一个类。能否更具体一些?这会有什么帮助?感谢您的输入!非常感谢!效果非常好!
p.find('strong, b, i').remove();