Html和javascript代码拼接问题 [英] Html and javascript code Concatenation problem

查看:35
本文介绍了Html和javascript代码拼接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 javascript 和 onClick 附加 HTML 语法,尝试传递带有 2 个参数的函数.但是在传递参数时,我在连接方面做错了.请帮忙.这是附加代码.

I am trying to append HTML syntax with javascript and onClick trying to pass a function with 2 parameters. But while passing the parameters I am doing something wrong in concatenation. Please help.Here is the append code.

addPanelHtml += "<li><a href='#' id='btnhistory" + i+ "' onclick='histDrpDwnEdt("+chkval+"','" + i+ ")'>History Parameters</a></li>";

运行程序时出现错误:

未捕获的语法错误:意外的输入结束

推荐答案

我强烈建议在这个用例中使用模板字符串

I would highly recommend to use template strings in this use case

这里是模板字符串的解决方案

Here the solution with template strings

addPanelHtml += `<li><a href='#' id='btnhistory${i}' onclick='histDrpDwnEdt(${chkval}, ${i})'>History Parameters</a></li>`;

然后你就可以去掉所有这些+""+ 连接,只需使用 ${} 将变量括起来.

Then you can get rid of all these + " " + concatenations and just use ${} to enclose your variables.

这大大提高了代码的可读性.

This improves the readabilty of your code by far.

在这里你可以阅读更多关于模板字符串的信息

Here you can read more about template strings

这篇关于Html和javascript代码拼接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆