2011-05-06 16 views
7

Estoy tratando de establecer texto en una etiqueta dinámicamente mediante jQuery. Pero no puedo obtener <br> o \n para renderizar y darme nuevas líneas. Todo aparece en la misma línea y wraps.html label inserting newline

aquí es mi código On JsFiddle

Mi html:

<label id="myLabel" /> 

Mi Javascript:

$("#myLabel").text("This is my first line \n This should be my second line."); 

Respuesta

20

text() escapará ningún código html, por lo que en lugar de utilizar html()

$("#myLabel").html("This is my first line <br /> This should be my second line."); 
1

probar este lugar:

$('#myLabel') 
    .html('this is my first line<br/>This should be my second line.'); 
3

El problema es que .text() ignorará su html. Debería usar .html(), soy u puede poner cualquier html que desee dentro de un elemento. Entonces usted tendría:

$('#myLabel').html('This is my first line <br/> This should be my second line.'); 

Espero que esto ayude. Saludos