1

我只是在这里解决了一些问题,没有太多运气,我想知道是否有一小段语法可以在多行中分解一行代码,所以我最终不会得到一段非常长的代码。

我在 Jquery 和段落中添加了一些元素,这样使它延伸得很远,所以我只是希望它看起来不那么凌乱。

$('#image_holder').append('<div id="holder_info"><h5>Creatology Concept Design Academy     (Final College Yeah Exibition):</h5><p>For my final year at Weston College, we were asked to     invent a company and produce a series of designs related, this included</p></div>');

我还没有完成元素,只是想找到一种方法来整理它。

4

2 回答 2

3

您可以将一条语句分成几行而不需要做任何特别的事情。只需将分号 ( ;) 放在语句的末尾,以便清楚它应该在哪里结束。

当一行没有以分号结尾时,JS 会查看接下来的内容,看看在哪里插入分号并结束语句是有意义的。((某种)例外是return。)

如果您想拆分一个长字符串,只需将其拆分为较小的字符串并连接即可。

您发布的示例:

$('#image_holder').append('<div id="holder_info"><h5>Creatology Concept Design Academy     (Final College Yeah Exibition):</h5><p>For my final year at Weston College, we were asked to     invent a company and produce a series of designs related, this included</p></div>');

很容易变成:

$('#image_holder')
    .append(
        '<div id="holder_info"><h5>Creatology Concept Design Academy     ' +
        '(Final College Yeah Exibition):</h5>' +
        '<p>For my final year at Weston College, we were asked to     ' +
        'invent a company and produce a series of designs related, ' +
        'this included</p></div>'
    );

(缩进只是风格问题,不是要求。)

这是有效的,因为 JS 不能在这些行的任何地方插入分号,并且分号两侧的代码在语法上是有意义的。

这不起作用的原因

return
    true;

或者

return
    this;

是因为return;can 本身就是一个语句,所以 can trueor thisor 后面的任何东西return,所以 JS 在之后插入一个分号return。这并不是一个真正的例外,只是需要注意更多潜在的陷阱。

于 2013-06-03T14:25:17.373 回答
2

您可以关闭字符串并与 连接+,并将换行符放在您想要的任何位置(字符串之外)。

$('#image_holder').append('<div id="holder_info"><h5>' 
 + 'Creatology Concept Design Academy' 
 + '     (Final College Yeah Exibition):</h5>' 
 + '<p>For my final year at Weston College,' 
 + ' we were asked to     invent a company and' 
 + ' produce a series of designs related, this included</p></div>');
于 2013-06-03T14:24:23.743 回答