我正在编写一个简单的程序,它使用 HTML 表单来填充一些信息的数组,到目前为止,我可以获取输入数据,将其存储到我的列表中,并将其可视化,如下所示:
它基本上将名称转换为链接,当您单击它时,它将创建一个 <div>
其中我显示了联系人的所有信息。
我是这样做的:
我收到的错误出现在代码的最后 6 行中。
(我试图避免所有不麻烦的代码)
var list = [];
var ulList = document.createElement("UL");
function AddToList(){
//Just pushes the info into the list.
}
function Visualize(){
ClearScreen();
for(var i = 0 ; i < list.length ; i++){
//Tried to keep it clean, this just works with each item in the list.
AddToList(i);
}
}
//This Works correctly, it shows every Name i've previously pushed into the list like a Link.
function AddToList(index){
var element = document.createElement("LI");
var name = document.createTextNode(list[index].name);
element.appendChild(name);
var link = document.createElement("A");
link.setAttribute("HREF", "#");
link.appendChild(element);
lik.setAttribute("ID", index);
link.addEventListener("click", ShowInfo(this.id)); //Index would do the same
ulList.appendChild(link);
document.getElementsByTagName("body")[0].appendChild(ulList);
}
//Trouble comes here
function ShowInfo(index){
CleanDIV();
//Previously created <div> with box as id
var box = document.getElementById("box");
var details = document.createElement("UL");
var lName = document.createElement("LI");
var lNumber = document.createElement("LI");
var lMail = document.createElement("LI");
//
//The error is here : Cannot Read Property 'name' of undefined
//And i dont know why, since i've done something similar in a previous line...
//
lName.appendChild(document.createTextNode("Name :" + list[index].name));
lNumber.appendChild(document.createTextNode("Number : " + list[index].number));
lMail.appendChild(document.createTextNode("Mail : " + list[index].mail));
details.appendChild(lName);
details.appendChild(lNumber);
detaisl.appendChild(lMail);
}
我什至不知道我犯了什么样的错误,也不知道如何问这个问题。
对于任何语法错误、我糟糕的变量命名能力以及我的问题质量的任何缺乏,我深表歉意。
谢谢你。
最佳答案
在您的 AddToList 函数中,索引为“未定义”
关于javascript - 如何访问对象属性数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879353/