简体   繁体   English

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

[英]JavaScript string literal in object literal syntax error

I am trying to put some html mark-up inside an array for retrieval later. 我试图在数组中放入一些html标记以便稍后检索。 My editor throws a syntax error on the description1 line, I can't figure out why. 我的编辑器在description1行上抛出语法错误,我无法弄清楚原因。 Any help would be much appreciated. 任何帮助将非常感激。 Code below. 代码如下。 Thanks A 谢谢

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-->'
} 

You have an unclosed string literal. 你有一个未闭合的字符串文字。 JavaScript strings are not multi line by default. 默认情况下,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-->'
} 

(fiddle) (小提琴)

Alternatively, you can create multi line string by using the \\ character, those only work in newer implementations. 或者,您可以使用\\字符创建多行字符串,这些字符串仅适用于较新的实现。 See this related question or the language specification . 请参阅此相关问题语言规范

Note: It's usually not the best idea to store HTML in strings, it makes it harder to debug and work with. 注意:将HTML存储在字符串中通常不是最好的主意,这使得调试和使用更加困难。 You can usually use templates. 您通常可以使用模板。 It's not that there are no good use cases, it's just rarely the case. 并不是说没有好的用例,只是很少见。

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

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