[英]My innerHTML is generating content from my arrays as literal text. I want the data inside the array to generate
With this code, my loop runs through my array and generates fine in the alert. 使用此代码,我的循环遍历数组,并在警报中正常生成。 But when I use innerHTML it produces a actual text and not the data.
但是当我使用innerHTML时,它会生成实际的文本,而不是数据。 What am I doing wrong?
我究竟做错了什么?
var myArray = [];
myArray[0] = {name:"John", value:"Doe"};
myArray[1] = {name:"Jane", value:"Dew"};
myArray[2] = {name:"James", value:"Dot"};
myArray[3] = {name:"Jack", value:"Dax"};
for (var i=0; i < myArray.length; i++) {
//alert(myArray[i].name + '/' + myArray[i].value);
}
function byId(id) {
return document.getElementById(id);
}
byId("full").innerHTML = "myArray[i].name + '/' + myArray[i].value;";
You have to do: 你必须做:
.innerHTML = myArray[i].name + '/' + myArray[i].value;
Your code didn't work because you were outputing your js code as a string due to the double quotes. 您的代码无效,因为由于双引号,您将js代码输出为字符串。
If you want to show all the item values in the div, you'd have to do something like: 如果要显示div中的所有项目值,则必须执行以下操作:
var text = [],
for (var i=0; i < myArray.length; i++) {
text.push( myArray[i].name + '/' + myArray[i].value);
}
byId("full").innerHTML = text.join('<br/>');
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.