admin管理员组

文章数量:1391934

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: Unexpected token ILLEGAL".

How can I fix this problem? I don't want to write the code on 1 line :)

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: Unexpected token ILLEGAL".

How can I fix this problem? I don't want to write the code on 1 line :)

Share Improve this question edited Nov 29, 2010 at 17:02 markzzz asked Nov 29, 2010 at 16:48 markzzzmarkzzz 48.1k126 gold badges319 silver badges534 bronze badges 2
  • What happens if you removed the extra '/'? My guess would be that it should work. It will only appear as 1 line in the rendered HTML, but that isn't where readability counts anyway (neither a user nor developer view). – Thomas Langston Commented Nov 29, 2010 at 16:52
  • 1 by the way, this has nothing to do with jQuery, just JavaScript string syntax. – Matt Commented Nov 29, 2010 at 16:53
Add a ment  | 

2 Answers 2

Reset to default 11

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.

本文标签: syntaxJavascriptHow can I write my clean codeStack Overflow