如何使以下JavaScript代码工作并生成列表 ID为“myDivId”的元素中的名称和年龄:
<div id="myDivId">No results yet</div>
<script>
new PersonPrinter("myDivId",[{"name":"Joe","age":"23"}
{"name":"Sam","age":"31"}]).render();
</script>`
答案 0 :(得分:1)
In the PersonPrinter render function you need to update the div
PersonPrinter.prototype.render = function(id,data) {
var elem = document.elementById(id);
for (var i =0;i <data.length;i++) {
var d = dcoument.createElement('div');
d.appendChild(document.createTextNode('Name'+data.name +' Age:'+data.age');
elem.appendChild(d);`enter code here`
}
}
答案 1 :(得分:0)
您需要使用innerHTML
访问div idvar a = Document.getElementById('myDinId');
为了改变价值,只需使用=运算符
a.innerHTML = 'Something';