请教,为什么这样写的时候结构体没法当作参数传输?

为什么我要选择编程 2018-06-09 09:22:17
为什么这段程序没办法把a这个结构体传送过去?错误代码显示:Unexpected identifier。有没有什么方法可以改的。现在我只能先把参数存在localStorage中再读取凑合用。。。

<html>
<head>
<script>
function model(){
this.a=1;
this.b=2;
}

function test(a){
alert(a);
}
</script>
</head>
<body>
<script>
var a=new model();
document.write('<button onclick="test('+a+')">aaa</button>');
//test(a);
</script>
</body>
</html>
...全文
123 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ambit_tsai-微信 2018-06-09
  • 打赏
  • 举报
回复

'<button onclick="test('+a+')">aaa</button>'
因为上面的写法,a会被转成字符串,不会执行。 你可以这么写:

'<button onclick="test(window.a)">aaa</button>'
  • 打赏
  • 举报
回复
如果把document.write('<button onclick="test('+a+')">aaa</button>');改成document.write('<button onclick="test('+a.a+')">aaa</button>');就能执行。

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧