本文介绍了document.getElementById和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java语言的新手,我想使用document.getElementById来显示项目的值,在我的示例中,我想列出在var = btsFrontEnd中打印的名称,但是我做错了什么.任何帮助都感激不尽.

I am new to Javascript and I would like to use the document.getElementById to show the value of items, in my example I would like to list the names printed in the var= btsFrontEnd ,but I am doing something wrong. Any help will be much appreciated.

谢谢.链接到我的小提琴

var btsFrontEnd = {
     "employee-1": {
         "name": "Name One",
         "phone": "1234567890",
         "email": "[email protected]"
        },

    "employee-2": {
                  "name": "Name Two",
                  "phone": "1234567890",
                  "email": "blah@blah."
     }
};


var btsemployees = {
    employees:[
        {
     "name": "Name One",
     "phone": "1234567890",
     "email": "[email protected]"
    },
     {
     "name": "Name Two",
      "phone": "1234567890",
     "email": "[email protected]"
},
     {
     "name": "Name Three",
      "phone": "1234567890",
     "email": "[email protected]"
},
     {
     "name": "Name Four",
      "phone": "1234567890",
     "email": "[email protected]"
},
     {
     "name": "Name Five",
      "phone": "1234567890",
     "email": "[email protected]"
}
]
};


//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/

Here is the 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(",");

这篇关于document.getElementById和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:55