2017-06-05 11 views
1

この不愉快な長い行のテキストを段落に変換する方法はありますか?JQuery .text()複数行

前:

$('.ContText-1').text('For a business to thrive, it needs a team of people who are dedicated to its success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of that team for each of their clients.'); 

後:(後の単一引用符にUncaught SyntaxError: Invalid or unexpected token
その参照:私は段落のいずれかを実行した場合

$('.ContText-1').text(' 
      For a business to thrive, it needs a team of people who are dedicated to its 
     success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of 
     that team for each of their clients. 
'); 

私はこのエラーを取得します。

代わり text()方法の使用の

答えて

1

、あなたは文字列を区切ることにより、リテラルのテンプレートを使用することができます`と:

$('.ContText-1').text(` 
    For a business to thrive, it needs a team of people who are dedicated to its 
    success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of 
    that team for each of their clients. 
`); 

注他のすべての近代的なブラウザでの作品があるが、これは、IEのすべてのバージョンで完全にサポートされていないこと - も端。

別の方法としては、個別の行を追加することができます

$('.ContText-1').text(
    'For a business to thrive, it needs a team of people who are dedicated to its ' + 
    'success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of ' + 
    'that team for each of their clients.' 
); 
+0

でエスケープしないので、それはうまくいきません。 –

0

html()方法とライン

$('.ContText-1').html('<br/>For a business to thrive, it needs a team of people who are dedicated to its <br/>success. Paul Lister, Bryan Jeter, and Bryan Lloyd are committed to being a part of <br/> that team for each of their clients.'); 
+0

問題は、段落をイマイチ。問題はコードエディタにあります。長いコード行が醜いように見えます。 –

0

の代わりに 'で文字列を囲むを破るために<br/>を使用し、 `使用するようにしてください。

少なくともJSの新しい反復では、これは正常に動作するはずです。

UPDATE:実は、これは前にカバーされていた

:出力、あなたのコード内の文字列を破るしようとしているではないと仮定すると、Creating multiline strings in JavaScript

+0

文字列に一重引用符があり、 '\'が ' –