Javascript 非法令牌错误 [英] Javascript Illegal Token Error
问题描述
请原谅我,如果这是一个简单的问题,但我似乎无法找到此代码的原因:
Forgive me if this is a simple problem but I can't seem to find why this code:
function create_content(c)
{
var html = "<div id='header'>"+c+"</div>";
if(c == "links")
{
var ul = "<ul><li><a href='http://www.mylink.com'>My Link 1</a></li>
<li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
html = html + ul;
}
return(html);
}
在 Chrome (win) 中给我这个错误:
Is giving me this error in Chrome (win):
未捕获的语法错误:意外令牌非法
Uncaught SyntaxError: Unexpected token ILLEGAL
在以var ul ="开头的行
On the line that starts with "var ul = "
任何建议都会有所帮助,谢谢!
Any advice would help thanks!
推荐答案
您正在 ul
字符串中插入一个换行符,在结束的 </li>
之间和开头 .JavaScript 字符串文字本身不能像这样跨越多行,除非你
You are inserting a line break in your ul
string, between the closing </li>
and the opening <li>
. JavaScript string literals cannot span multiple lines like this by themselves, unless you
在除最后一行之外的每一行都添加一个
\
(正如 Ivo Wetzel 所说):
Trail a
\
at each line but the last (as Ivo Wetzel says):
var ul = "<ul><li><a href='http://www.mylink.com'>My Link 1</a></li>\
<li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
将它们分开并连接起来,如下所示:
Break them and concatenate the parts, like this:
var ul = "<ul><li><a href='http://www.mylink.com'>My Link 1</a></li>";
ul += "<li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
(为了保持换行符,你需要在某处放置一个 \n
,但在 HTML 中这无关紧要.)
(To keep the newline there you would place a \n
somewhere, but in HTML it won't matter.)
这篇关于Javascript 非法令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!