我是 Javascript 新手,我想使用 document.getElementById 来显示项目的值,在我的示例中,我想列出 var= btsFrontEnd 中打印的名称,但我做错了。任何帮助都感激不尽。
var btsFrontEnd = {
"employee-1": {
"name": "Name One",
"phone": "1234567890",
"email": "blah@blah.com"
},
"employee-2": {
"name": "Name Two",
"phone": "1234567890",
"email": "blah@blah."
}
};
var btsemployees = {
employees:[
{
"name": "Name One",
"phone": "1234567890",
"email": "blah@blah.com"
},
{
"name": "Name Two",
"phone": "1234567890",
"email": "blah@blah.com"
},
{
"name": "Name Three",
"phone": "1234567890",
"email": "blah@blah.com"
},
{
"name": "Name Four",
"phone": "1234567890",
"email": "blah@blah.com"
},
{
"name": "Name Five",
"phone": "1234567890",
"email": "blah@blah.com"
}
]
};
//First argument is our data structure (an object or an array
//Second argument is a callback (logic we apply to our data structure)
$.each(btsFrontEnd, function (key, value) {
console.log(key); //Prints this object's keys
console.log(value); //Prints immediate values from this object
console.log(btsFrontEnd[key]);
console.log(value.name);
document.getElementById("names").innerHTML.value;// This is what I am referring to, I would like it to appear in the p id="names"
});
最佳答案
这是 jsfiddle:http://jsfiddle.net/Ss2kk/7/
// Put names into an array
var employeeNames = [];
$.each(btsFrontEnd, function (employeeid, employee) { //first is the key or index, second argument is the value
// Check each element if it has name field
if (employee.name !== undefined) {
// Put its name into the array
employeeNames.push(employee.name);
}
});
// Join the array as comma seperated and put the content into `<p>` tag.
document.getElementById("names").innerHTML = employeeNames.join(",");
关于javascript - document.getElementById 和 Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17411947/