原创地址:http://my.oschina.net/xinyuan6009/blog/164418
js异常:javascript Firebug error: Identifier starts immediately after numeric literal
异常原因:标识符以数字开头异常分析:
示例1·
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
>
<
title
>Untitled Document</
title
>
<
script
>
function test(id,content){
alert(id);
}
</
script
>
</
head
>
<
body
>
<
button
onclick
=
"test(1,this);"
>测试</
button
>
</
body
>
</
html
>
|
示例2·
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
>
<
title
>Untitled Document</
title
>
<
script
>
function test(id,content){
alert(id);
}
</
script
>
</
head
>
<
body
>
<
button
onclick
=
"test(1aaa,this);"
>测试</
button
>
</
body
>
</
html
>
|
从上面可以看出:js脚本中,函数参数可以是以下几种情况:
1·字符串
2·数字
3·定义好的js对象
示例2中参数之一是1aaa,这样的参数本首先当作一个对象识别,但是js标识符并不能以数字开头。所以报错
解决办法:在参数传入之前将其转换为字符串,形式如下test('1aaa',this);
但是这种情况下,如果test函数是由js脚本动态生成,也就是说test函数本身就是由字符串拼成的会出现一种情况,就是拼接后生成的代码出现错位。。。很诡异,不知道原因。解决办法是将字符串中的函数用\"test('"+1aaa+"')\"包起来。
参考文章:
http://stackoverflow.com/questions/14966133/syntaxerror-identifier-starts-immediately-after-numeric-literal-in-firebug
http://stackoverflow.com/questions/17358351/syntaxerror-identifier-starts-immediately-after-numeric-literal
http://stackoverflow.com/questions/5883397/javascript-firebug-error-identifier-starts-immediately-after-numeric-literal