错误:SyntaxError: identifier starts immediately after numeric literal

转载:http://blog.csdn.net/shalousun/article/details/39995443
在用JavaScript时,当你使用一个字符传作为函数的参数常常会看到语法错误,在firebug下会报SyntaxError: identifier starts immediately after numeric literal ,当然在google下提示就不准确了。 错误原因是:标识符以数字开头 下面直接看例子吧: $(function(){
var str = "509edbe9-2914-431f-9128-97d368b7da0b"; //错误的写法 var html = '<button class="button" id="ensure" onclick="test(str)">确定</button>';//把字符串作为参数传给函数,直接报错 //正确的写法 var html = '<button class="button" id="ensure" onclick="test(\''+str+'\')">确定</button>';//正确执行,注意第一个\后是两个单引号 $("#dd").append(html); }); function test(id){ console.log(id); } <div id="dd"></div>

 

posted @ 2017-04-07 09:13  雷雨天的雷胖子  阅读(1049)  评论(0编辑  收藏  举报