How to make the following JavaScript code work and result in a list of names and ages inside the element with ID "myDivId" :
<div id="myDivId">No results yet</div>
<script>
new PersonPrinter("myDivId",[{"name":"Joe","age":"23"}
{"name":"Sam","age":"31"}]).render();
</script>`
最佳答案
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`
}
}
关于javascript - 如何使以下 JavaScript 代码正常工作并在 ID 为 "myDivId": 的元素内生成姓名和年龄列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28009912/