简体   繁体   English

Html和javascript代码拼接问题

[英]Html and javascript code Concatenation problem

I am trying to append HTML syntax with javascript and onClick trying to pass a function with 2 parameters. 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.请帮助。这是 append 代码。

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

while running the program I am getting error:运行程序时出现错误:

Uncaught SyntaxError: Unexpected end of input未捕获的 SyntaxError:输入意外结束

Try instead of writting the ' , try like this \' :尝试而不是写' ,尝试像这样\'

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在这里您可以阅读有关模板字符串的更多信息

My suggestion:我的建议:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM