为什么这个方法在控制台中返回所有对的属性:对象的值,但对于document.getElementById("myDiv").innerHTML
,它只返回最后一对属性:对象的值?如何在HTML文件中显示所有对?
var obj = {"prop1": "value1", "2017-12-14 16:00:00": {"prop2": "value2", "prop3": "value3"}};
var d = new Date(),
ddPrev = d.getDate()-1,
mm = d.getMonth()+1,
yyyy = d.getFullYear(),
hh = d.getHours(),
min = d.getMinutes();
if(mm < 10) mm = '0' + mm;
if(min < 10) min = '0' + min;
if(ddPrev < 10) ddPrev = '0' + ddPrev;
var dOpt = yyyy + '-' + mm + '-' + ddPrev + ' 16:00:00';
var txt = obj[dOpt];
window.onload = function() {
Object.getOwnPropertyNames(txt).forEach( function (val, idx, array) {
document.getElementById("myDiv").innerHTML = val + ': ' + txt[val];
});
}
&#13;
<body><div id="myDiv"></div></body>
&#13;
谢谢!