[英]javascript: onclick function is not working on email
i have a javascript and inside the ajax of datatables, i have this part of code我有一个 javascript,在数据表的 ajax 里面,我有这部分代码
{ "data": null,
"width": "10%",
"render": function(data){
icon2 = '<center><button type="button" class="btn btn-info m-btn m-btn--icon m-btn--icon-only btn-sm" ' +
'data-toggle="m-tooltip" title="" onclick="sendEmail('+data.email+')" data-placement="top" data-original-title="' + 'Send Email' + ' "> <i class="la la-envelope"></i></button>';
icon2 += '</center>';
return icon2; }
}
and a function和一个函数
function sendEmail(email){
console.log("email: ", email);
}
but when i click the button, there is an error says **Uncaught SyntaxError: missing ) after argument list **.但是当我单击该按钮时,在参数列表 ** 之后出现错误提示 **Uncaught SyntaxError: missing )。
is there any other solution for this?有没有其他解决方案?
It looks like the error is in the "render": function(data){
portion.看起来错误出在"render": function(data){
部分。 That function is closed but the enclosing hash is not.该函数已关闭,但封闭的散列未关闭。
{ "data": null,
"width": "10%",
"render": function(data){
icon2 = '<center><button type="button" class="btn btn-info m-btn m-btn--icon m-btn--icon-only btn-sm" ' +
'data-toggle="m-tooltip" title="" onclick="sendEmail('+data.email+')" data-placement="top" data-original-title="' + 'Send Email' + ' "> <i class="la la-envelope"></i></button>';
icon2 += '</center>';
return icon2; }
}
Note the extra }
at the end.注意最后的额外}
。
Since literals have to be quoted .由于必须引用文字。
You can't just write:你不能只写:
onclick="sendEmail(bob@example.com)"
This quick and dirty approach is just to add a couple of '
to the data, but that makes the assumption that there won't be anything else problematic about the data (such as it including '
, "
, or new lines).这种快速而肮脏的方法只是向数据添加几个'
,但这假设数据不会有任何其他问题(例如它包括'
、 "
或新行)。
You should:你应该:
eg例如
const email = data.email;
const js_safe_email = JSON.stringify(email);
const html_safe_email = js_safe_email
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
icon2 = '<center><button type="button" class="btn btn-info m-btn m-btn--icon m-btn--icon-only btn-sm" ' +
'data-toggle="m-tooltip" title="" onclick="sendEmail('+html_safe_email+')" data-placement="top" data-original-title="' + 'Send Email' + ' "> <i class="la la-envelope"></i></button>';
This is rather horrible, but so is mashing together strings of data embedded in JS embedded in HTML.这是相当可怕的,但将嵌入在 HTML 中的 JS 中嵌入的数据字符串混合在一起也是如此。 It's generally better to use DOM methods instead.通常最好使用 DOM 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.