2017-08-29 8 views
0

<img>タグが入れ子になっているもの以外のすべての<p>タグを削除することはできますか?jquery?jQueryタグを使用して削除するには:not not operator?

<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> 

それは出力だけでイメージする必要があります意味:

は、ここに私の問題のコードです!

答えて

3

あなたは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>
を使用するようにするために、これを達成するために :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>

+0

ouu:has()はすべてを解決します –

0

を別の方法を使用することができます