Javascript - 如何编写干净的代码?

标签 javascript syntax

我需要在 Jquery 上做一个函数,在 div 中写入一些 HTML 元素。

代码是这样的:

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' />");
}

我需要尽可能干净地清理代码,所以我已经对齐它(我在写一行时转到新行)。

我知道在 Javascript 上我可以做到,在每行末尾添加一个 /。但也许我记错了。事实上,如果我在 Chrome 上运行这段代码,控制台会显示“Uncaught SyntaxError: Unexpected token ILLEGAL”。

我该如何解决这个问题?我不想把代码写在 1 行 :)

最佳答案

您需要在每行末尾使用反斜杠而不是正斜杠:

var s = "one\
         two\
         three";

关于Javascript - 如何编写干净的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305944/

相关文章:

programming-languages - 为什么大多数编程语言中没有更多的控制结构?

javascript - 在循环中声明的变量和重新使用的变量会发生什么

javascript - 如何在 Bootstrap 中统计消息/警报指示器的总数?

syntax - 如何解决奇怪的 Haxe 编译器错误?

error-handling - 使用 future 时 Either 是否有更符合人体工程学的语法?

perl - Perl 中@array->[4] 或 %hash->{key} 行为的文档在哪里?

syntax - 在 CoffeeScript 中初始化数组的最聪明方法

javascript - 点击重叠的 div

javascript - mapStateToProps 返回未定义

javascript - typeahead.min.js Github 链接不工作