Bungkus/Pisahkan Setiap Baris Kata Dengan Elemen - Dewa Blogger

Halaman

    Social Items

Buy Now

Bungkus/Pisahkan Setiap Baris Kata Dengan Elemen

$('div').each(function() {     var d = $(this).html();     $(this).html('<p>' + d.replace(/[\r\n]+(?=[^\r\n])/g, '</p><p>') + '</p>'); });

Kode di atas akan mengubah sekelompok teks yang tadinya ibarat ini:

<div>Lorem ipsum dolor sit amet consectetuer adipiscing elit.</div>

menjadi ibarat ini:

<div><p>Lorem ipsum</p> <p>dolor sit amet</p> <p>consectetuer</p> <p>adipiscing elit.</p></div>

Lihat Demo

Namun instruksi di atas hanya berlaku untuk ganti baris berupa \r dan \n. Jika Anda sedang berafiliasi dengan ganti baris berupa elemen <br>, Anda dapat memakai ini:

$('div').contents().filter(function() {     // Select all textnodes     return this.nodeType == 3; }).wrap('<p></p>'); // Place them inside paragraph elements $('br', 'div').remove(); // Remove the line-break

Lihat Demo


  1. Stackoverflow - How to Strip Out <br> Tags and Wrap Lines with <p> and </p> Tags?
  2. Stackoverflow - jQuery, Remove New Line Then Wrap Textnodes with <p>

Sumber https://www.dte.web.id/