2016-05-04 6 views
1

タグの挿入方法
テキストの最初の部分の後にタグを挿入するには?例えば挿入点ポイントjqueryの後

私は

<p>Some text here. Some text after point.</p> 

結果

<p>Some text here.<br> Some text after point.</p> 
+0

は常にのみ、最大2点ありますを与えるだろう? –

答えて

1

を持っているあなたはbr要素を追加し、pタグのHTMLにテキスト全体を入れてreplaceを使用し、その後p要素のテキストを読むことができます。

$(function(){ 
 
var text = $('p').text(); 
 
    text = text.replace('here.','here.<br>'); 
 
    $('p').html(text); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>Some text here. Some text after point.</p>

0

あなたは正規表現を使用する必要があります。

"<p>Some text here. Some text after point.</p>".replace(/(\.\s+)/g, "$1<br/>") 

これは次のように出力

"<p>Some text here. <br/>Some text after point.</p>" 
関連する問題