对象文字语法错误中的JavaScript字符串文字

Ant*_*Ant 5 javascript

我试图在数组中放入一些html标记以便稍后检索.我的编辑器在description1行上抛出语法错误,我无法弄清楚原因.任何帮助将非常感激.代码如下.谢谢

var modalcontent = {
  description1 : '<div id = "description"><div class = "tbc">  
  <label class = "tbc" for = "tbc">Description</label>
  </div>
  <div class = "tbc">
  <input type = "text" class = "tbc" name = "description" id = "tbc" placeholder =        "Enter description">
  </div>
</div>
<!--end description div-->'
} 
Run Code Online (Sandbox Code Playgroud)

Ben*_*aum 6

你有一个未闭合的字符串文字.默认情况下,JavaScript字符串不是多行.

var modalcontent = {
  description1 : '<div id = "description"><div class = "tbc"> '+
  '<label class = "tbc" for = "tbc">Description</label>'+
  '</div>'+
  '<div class = "tbc">'+
  '<input type = "text" class = "tbc" name = "description" id = "tbc" placeholder = "Enter description">'+
  '</div>'+
  '</div>'+
  '<!--end description div-->'
} 
Run Code Online (Sandbox Code Playgroud)

(小提琴)

或者,您可以使用\角色创建多行字符串,这些字符仅适用于较新的实现.请参阅此相关问题语言规范.

注意:将HTML存储在字符串中通常不是最好的主意,这使得调试和使用更加困难.您通常可以使用模板.并不是说没有好的用例,只是很少见.