JavaScript onclick-不正确的解释
将onclick事件存储到页面的html代码时出现问题。 当表格充满行时,JavaScript onclick-不正确的解释,javascript,jquery,html,Javascript,Jquery,Html,将onclick事件存储到页面的html代码时出现问题。 当表格充满行时,tr上的产品名称应如下所示: <tr onclick="cart_pr('0K2FA-50030XX')">...</tr> <tr onclick="cart_pr('1000" fa')>...</tr> function FillArticles() { //--------- Request to DB -----------// //---
tr
上的产品名称应如下所示:
<tr onclick="cart_pr('0K2FA-50030XX')">...</tr>
<tr onclick="cart_pr('1000" fa')>...</tr>
function FillArticles()
{
//--------- Request to DB -----------//
//--------- Parse json etc.
for(i=0;i<json.length;i++)
{
//----- Here goes loop to fill a table ----//
res.push("<tr onclick=cart_pr('"+obj.name+"')></tr>");
//----- I don't show more, cause only line above is important, so....
//----- below Closes the row with </tr>
res.push("</tr>");
}
$("#mytable").html(res.join(''));
//--------- In this line in debugger, res is absolutely OK
}
。。。
所有文章中只有一篇(名为“1000 fa”)错误地显示如下:
<tr onclick="cart_pr('0K2FA-50030XX')">...</tr>
<tr onclick="cart_pr('1000" fa')>...</tr>
function FillArticles()
{
//--------- Request to DB -----------//
//--------- Parse json etc.
for(i=0;i<json.length;i++)
{
//----- Here goes loop to fill a table ----//
res.push("<tr onclick=cart_pr('"+obj.name+"')></tr>");
//----- I don't show more, cause only line above is important, so....
//----- below Closes the row with </tr>
res.push("</tr>");
}
$("#mytable").html(res.join(''));
//--------- In this line in debugger, res is absolutely OK
}
我需要添加引号&39代码>而不是”
res.push("<tr onclick='cart_pr('"+obj.name+"');'></tr>
res.push(“
但这肯定不是制作具有可单击行的表的最佳方法,特别是在使用具有多个参数的函数时
最好在JQuery中使用.on()
而不是这种方法。在obj.name
中使用双引号和单引号转义\n不要添加内联事件处理程序,尤其是在您已经在使用JQuery时!低调地使用javascript。避免使用onclick属性绑定事件,而是使用.on()
把那些该死的花括号修好!天哪,我的眼睛hurt@jjj他仍然需要将字符串作为数据属性存储在元素上,这样就不会为他保存任何代码。