EspañolDeutschFrançaisItalianoPortuguêsРусскийΕλληνικά日本語中文(简体)हिन्दी; हिंदीالعربية
Definir como idioma padrão
Perguntas de PF - Fórum

Como faço para substituir espaços por tags dentro de uma marca ?

Como posso substituir, dentro de um rótulo <Rótulo>, o Espaços existente por rótulos <Br />?. Por exemplo, Eu tenho o seguinte:

<label>Aquí va algún texto</label>

E eu quero trocá-lo por:

<label>Aquí<br/>va<br/>algún<br/>texto</label>

Eu tentei com:

$('label').html().replace(' ', '<br/>');

Mas não funciona para mim..

Share on Facebook Tweet about this on Twitter Share on Google+ Pin on Pinterest Share on Reddit Share on VK Share on StumbleUpon Share on Tumblr Share on LinkedIn Email this to someone Print this page
FishGG Será que perguntou 7 anos
2 Respostas
Melhor resposta

Você pode usar o método html() com uma função de retorno de chamada. Para o espaço branco você pode usar uma expressão regular no método replace().

$('label').html(function(i, oldHTML) {
   return oldHTML.replace(/\s+/g, '<br/>');
}

 

Share on Facebook Tweet about this on Twitter Share on Google+ Pin on Pinterest Share on Reddit Share on VK Share on StumbleUpon Share on Tumblr Share on LinkedIn Email this to someone Print this page
Borja disse faz 7 anos

Prova dessa forma:

$('label').html($('label').html().replace(' ', '<br/>'));
Share on Facebook Tweet about this on Twitter Share on Google+ Pin on Pinterest Share on Reddit Share on VK Share on StumbleUpon Share on Tumblr Share on LinkedIn Email this to someone Print this page
Juanr disse faz 7 anos