SyntaxError: identifier starts immediately after numeric literal
在用javascript时,当你使用一个字符传作为函数的参数常常会看到语法错误
错误原因是:标识符以数字开头
$(function(){
var str = "509edbe9-2914-431f-9128-97d368b7da0b";
//错误的写法
var html = '<button class="button" id="ensure" οnclick="test(str)">确定</button>';//把字符串作为参数传给函数,直接报错
//正确的写法
var html = '<button class="button" id="ensure" οnclick="test(\''+str+'\')">确定</button>';//正确执行,注意第一个\后是两个单引号
$("#dd").append(html);
});
function test(id){
console.log(id);
}
<div id="dd"></div>
htmlStr+="<a onclick=\"window.location.href='workbench/activity/quaryActivityAndRemark.do?id=3'\"></a>";
<a onclick="window.location.href='workbench/activity/quaryActivityAndRemark.do?id=3'"></a>
window.location.href相当于是一个函数
onclick=herf(aorkbench/activity/quaryActivityAndRemark.do?id=3)>//这样是错的,因为我们的参数a会被转换成97
onclick=herf('aorkbench/activity/quaryActivityAndRemark.do?id=3')>//这个好像只能使用单引号
————————————————
版权声明:本文为CSDN博主「shalousun」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shalousun/article/details/39995443