无论如何是否有可能删除所有<p>
标签,除了一个在嵌套内有 <img>
标记的人,带有jquery?
这是我的问题代码:
<script>
$(document).ready(function(){
$("p>img:not").remove();
});
</script>
<p><img src="Anyimg.jpg"></p>
<p>My best friend is Mickey.</p>
<p>Who is your favourite</p>
这意味着输出应该只是图像!
您可以使用:not()
和:has()
选择器
$("p:not(:has(img))").remove();
$("p:not(:has(img))").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><img src="Anyimg.jpg" alt="some image" /></p>
<p>My best friend is Mickey.</p>
<p>Who is your favourite</p>
另一种实现这一目标的方法来使用filter
方法
$("p").filter(function(){
return $('img',this).length == 0;
}).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><img src="Anyimg.jpg" alt="some image"></p>
<p>My best friend is Mickey.</p>
<p>Who is your favourite</p>