Syidentifier starts immediately after numeric l...

js异常:javascript Firebug error: Identifier starts immediately after numeric literal
异常原因:标识符以数字开头
异常分析:
示例1·
<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·
<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

转载于:https://my.oschina.net/xinyuan6009/blog/164418

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值