gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:12:10 25 4
gpt4 key购买 nike

我正在尝试将一些 html 标记放入数组中以供稍后检索。我的编辑器在 description1 行抛出一个语法错误,我不明白为什么。任何帮助将非常感激。下面的代码。谢谢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-->'
}

最佳答案

你有一个未闭合的字符串文字。默认情况下,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)

或者,您可以使用 \ 字符创建多行字符串,这些仅适用于较新的实现。参见 this related questionthe language specification .

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

关于对象文字语法错误中的 JavaScript 字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com