EspañolDeutschFrançaisItalianoPortuguêsРусскийΕλληνικά日本語中文(简体)हिन्दी; हिंदीالعربية
设置为默认语言
PF 问题 - 论坛

我如何可以代替那些空格标记内的标签呢? ?

¿Cómo puedo reemplazar, dentro de una etiqueta <label>, 的 espacios existentes por etiquetas <br />?. 举个例子, tengo lo siguiente:

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

y quiero cambiarlo por:

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

Lo he intentando con:

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

pero no me funciona.

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 preguntó 我们的一日三餐 7 年
2 答案
最佳答案

Puedes usar el metodo html() con una función de callback. Para los espacios en blanco puedes usar una expresión regular en el 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 说的得 7 年

这种形式的证明:

$('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 说的得 7 年