2016-11-21 8 views
1

を除外する私はコピーのセクションを選択するためにjqueryのを使用しています:選択pタグに、いくつかの子供たちに

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

(numが他の場所で宣言され、問題ではありません)。

これはpタグ内のすべてのコンテンツを選択します(明らかに)。ただし、選択しているpタグではタグと強力なタグがネストされています。 pタグを選択し、strongタグを除外する方法はありますか?私は以下のコード試してみました:

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

をしかし、これはすべての子要素(強い除く)ではなく、実際の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> 
+2

あなたは、例えば、HTMLと所望の出力が表示されてくださいことはできますか? – Stuart

+0

変更する要素にクラスを追加します。 –

+0

あなたはもっと具体的になることができますか?どのようにこれは正確に役立つだろうか?あなたのご意見ありがとうございます! – samisonline

答えて

3
var p = $('.main-content p:nth-child('+num+')').clone(); 
p.find('*:not(a)').remove(); 
var your_string = p.html(); 

または削除する正確なタグを指定することができます。

<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.find('strong, b, i').remove(); 
+0

ありがとう!チャームのように働いた! – samisonline

0

あなたは正規表現を使用することができます。

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>

関連する問題