Javascript does not allow literal line break characters inside strings.
There are several ways around the problem.
If you really want line breaks inside the string, use \n
:
$( '.qualquerClasse' ).html( 'conteúdo de 3 linhas de código: uma \n Duas \n Três' );
Note however that line-breaking characters within the HTML by default are not rendered (collapsed spaces) unless they are within an element whose computed value of the CSS property white-space
be it pre
/pre-wrap
/pre-line
. Otherwise (in most cases) it is necessary to use <br>
to force a visible line break.
If you want to break the text into several lines to make it more readable, you can concatenate or use an array:
$( '.qualquerClasse' ).html(
' conteúdo de 3 linhas de código: uma'
+ 'Duas'
+ 'Três'
);
//ou
$( '.qualquerClasse' ).html([
' conteúdo de 3 linhas de código: uma',
'Duas',
'Três'
].join(''));
Of course you can add \n
or <br>
as needed in these strings.
There is also a non-standard way to "escape" line breaks by placing a \
just before the literal line break:
$( '.qualquerClasse' ).html( ' conteúdo de 3 linhas de código: uma\
Duas\
Três'
);
This syntax is not standard, but has very good support. However, some browsers keep the line break characters inside the string while others discard it, so this shape is a bit inconsistent. Another problem is that if you add any character after the \
, even a space character, will generate a syntax error since the line break is no longer being escaped.
Ever tried to throw it all in one
string
only?– Felipe Avelar
Hello Thiago, do not forget to format posted codes, on the edit screen you just need to select the code and click the button
{}
(or press the shortcutCtrl
+K
)– Fabrício Matté
If you need to do this over and over again, it might be worth checking out a template scheme like Handlebars.
– elias
Thank you all for your help. I’m getting familiar with the functions of stackoverflow ( and also jQuery kkk), Fabrício Matté speechless, responded immediately and with fantastic alternatives. I will check which works best and then comment more. Zuul thanks for editing and formatting .
– Thiago Jem
@You were already an outsider in the best forms of web development in your time, 11 months ago, congratulations man! Template Engines was really what I needed, but at the time I didn’t even realize it; 2 months later I discovered and made a project with Node.js, express.js and jade.js got good, however, the best way I found, and what I currently use, is the Yeoman, learned the correct way to install and use watching the course of Jesus Christ, I no longer used template engine I put contents in arrays.
– Thiago Jem