0

我有 Array msg 我想将它传递给 html 中动态创建的元素的 onclick 函数。

for(var i=0; i < msg.length ; i++){

 categoryList += "<img src='../products/"+msg[i].imgurl+"' class='slide_contain' onclick='getCanvas("+msg[i].id+","+msg[i].shape+","+msg[i].height+","+msg[i].width+","+msg[i].topval+","+msg[i].leftval+","+msg[i].radius+","+msg[i].subcat+","+msg[i].imgurl+")'/>";
    categoryList += "<li> <span> "+msg[i].imgurl+"</span></li>";
}

检查时我看到:

<img src="../products/Lighthouse.jpg" class="slide_contain" onclick="getCanvas(2,3,100,100,100,210,100,1,Lighthouse.jpg)">

这里最后一个字符串变量没有正确传递??怎么做 ?或者否则我如何将 msg Array 传递给函数并在函数中获取值?

4

3 回答 3

1

你错过了引号..

 categoryList += "<img src='../products/"+msg[i].imgurl+"' class='slide_contain' onclick='getCanvas("+msg[i].id+","+msg[i].shape+","+msg[i].height+","+msg[i].width+","+msg[i].topval+","+msg[i].leftval+","+msg[i].radius+",'"+msg[i].subcat+"')'/>";
于 2013-10-08T06:01:37.397 回答
1

尝试这样的事情

categoryList += '<img src="../products/'+msg[i].imgurl+' class="slide_contain" onclick="getCanvas('+msg[i].id+','+msg[i].shape+','+msg[i].height+','+msg[i].width+','+msg[i].topval+','+msg[i].leftval+','+msg[i].radius+','+msg[i].subcat+',\''+msg[i].imgurl+'\')"/>';
于 2013-10-08T06:14:17.203 回答
0

以这种方式传递最后一个参数 ==>

  \'' msg[i].subcat  '\' 
 categoryList += "<img src='../products/"+msg[i].imgurl+"' class='slide_contain' onclick='getCanvas("+msg[i].id+","+msg[i].shape+","+msg[i].height+","+msg[i].width+","+msg[i].topval+","+msg[i].leftval+","+msg[i].radius+",' \''"+msg[i].subcat+"'\' ')'/>";

输出

'Lighthouse.jpg'
于 2013-10-08T06:11:24.287 回答