简体   繁体   English

vs代码在引号中换行多行

[英]vs code wrap multiple line in quote

I am new to vscode and and hardly get bothered by one thing.我是 vscode 的新手,几乎不会被一件事打扰。

 var template = "<div> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> </div> "

I have no clue why vscode is not wrapping all those line in quote.我不知道为什么 vscode 没有将所有这些行都包含在引号中。 It just throws a syntax error.它只是抛出一个语法错误。 It reads when they aligned in one line but when I break the line to make it look good, the code is not working.当它们排成一行时它会读取,但是当我打破该行以使其看起来不错时,代码不起作用。 Any idea?任何的想法?

You can do like this:你可以这样做:

 var template = ` <div> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> <input type="text" class="input"> </div> `; document.body.innerHTML = template;

If you aren't familiar with template strings here is the MDN documentation .如果您不熟悉模板字符串,请参阅 MDN 文档

You can try this way.你可以试试这个方法。 Just use single quotes to surround the variable string, and use backslashes to change lines.只需使用单引号将变量字符串括起来,并使用反斜杠来换行。

 var template = '<div>\ <input type="text" class="input"> \ <input type="text" class="input"> \ <input type="text" class="input"> \ <input type="text" class="input"> \ <input type="text" class="input"> \ </div>'; document.body.innerHTML = template;

It fails because of multiple reasons:由于多种原因,它失败了:

  1. You use double-quotes both to open and closing the variable aswell as the attributes.您可以使用双引号来打开和关闭变量以及属性。 The opening double-quote of the first attribute already closes the variable therefor.第一个属性的左双引号已经关闭了变量。
  2. A JS variable is not allowed to have linebreaks. JS 变量不允许有换行符。
  3. you do not close the variable with a semicolon or comma (in case more variables follow)您不要用分号或逗号关闭变量(以防后面有更多变量)

The only way you can fix it is like in the example below.修复它的唯一方法如下例所示。 You have to open and close the variable and use a + to continue the variable in the next line:您必须打开和关闭变量并使用+继续下一行中的变量:

 var template = '<div>' + '<input type="text" class="input">' + '<input type="text" class="input">' + '<input type="text" class="input">' + '<input type="text" class="input">' + '<input type="text" class="input">' + '</div>'; document.body.innerHTML = template;

or you can或者你可以

var template = '<div>\
                  <input type="text" class="input">\
                  <input type="text" class="input">\
                  <input type="text" class="input">\
                  <input type="text" class="input">\
                  <input type="text" class="input">\
                </div>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM