Javascript - How can I write my clean code?
I need to do a function on Jquery that write in a div some HTML elements.
Code is this :
function loadAppartamenti() {
$('.profilo_2').empty();
$('.profilo_2').html("<label class='article_span'>Inserisci Zona (es. Via/Località)</label> /
<input id='art_1' class='article_input' /> /
<label class='article_span'>Costo (€ Mese)</label> /
<input id='art_2' class='article_input' /> /
<label class='article_span'>Disponibilità (Periodo In Mesi)</label> /
<input id='art_3' class='article_input' /> /
<label class='article_span'>Condizioni</label> /
<input id='art_4' class='article_input' /> /
<label class='article_span'>Servizi Presenti</label> /
<input id='art_5' class='article_input' />");
}
I need to clean the code as clean as possible, so i've aligned it (I go to the new line when i write a line).
I know that on Javascript i can do it, adding a /
at the end of each line. But maybe i wrong remember. In fact if i run this code on Chrome, the console says "Uncaught SyntaxError: Unexpecte开发者_JAVA百科d token ILLEGAL".
How can I fix this problem? I don't want to write the code on 1 line :)
You need a backslash at the end of each line rather than a forward slash:
var s = "one\
two\
three";
Have you thought about
var textArray = [...]; // insert your text content
var portfilo = $(".profilo_2");
for (var i = 0; i < 5; i++) {
profilo.append(
$("<label>").addClass("article_span").text(textArray[i])
).append(
$("<input>").attr("id", "art_"+ i).addClass("input_span")
)
}
You can cache / optimise it some more if you care.
精彩评论