2017-03-17 10 views
0

</p></p>の間のスペースを取り除いて、タイトルをクリックすると.html();の内容が.question divに設定されるようにするにはどうすればよいですか? .html()の内容は動的に生成され、段落タグの間にスペースが入ります。私はどちらか正規表現または同様のものを実行することができるはずですが、$.trim()を使用すると、間ではなく、文字列の最初と最後でのみ動作します。jqueryで段落タグ間の空白を取り除く方法

<a id="title">click me</a> 
<div class="question"></div> 

$('#title').click(function() { 
    $('.question, .mobile-answer').html('<p class="header">Title</p><p>Content of question</p> 

    <p> more content goes here < /p> 

    <p> even more content goes here < /p> 
    ') 
    }); 

JSFIDDLE:LINK

答えて

0

あなたが</p><p>間の空白を削除するには、正規表現を使用することができます。動的なコンテンツと仮定するとcontentという変数である:

content = content.replace(/< *\/p *>\s+< *p *>/g, '<p></p>'); 
1

あなたの文字列の場合

str = '<p class="header">Title</p><p>Content of question</p> 

    <p> more content goes here < /p> 

    <p> even more content goes here < /p> 
    ' 

の操作を行います。

str.replace(/\s*(>|<)\s*/g,"$1") 

結果:

"<p class="header">Title</p><p>Content of question</p><p>more content goes here</p><p>even more content goes here</p>" 

https://regex101.com/r/DuEA31/2

0

SEOの問題により、コンテンツを表示するには、リンクをクリックしたときにdivとhide/showのトグル状態にする必要があります。これはまた、必要なデータが正規表現でフィルタリングされないようにします。

関連する問題