如何使用JavaScript调用数据文件,然后以表格形式显示
myObject.js如何使用JavaScript调用数据文件,然后以表格形式显示,javascript,html,Javascript,Html,myObject.js var myObject= {"customers":[ {"CustomerName" : "Alfreds Futterkiste","Country" : "Germany"}, {"CustomerName" : "Berglunds snabbköp","Country" : "Sweden&q
var myObject= {"customers":[
{"CustomerName" : "Alfreds Futterkiste","Country" : "Germany"},
{"CustomerName" : "Berglunds snabbköp","Country" : "Sweden"},
{"CustomerName" : "Centro comercial Moctezuma","Country" : "Mexico"}
]};
Display.html
<table>
<tr>
<th>#</th>
<th>CustomerName:</th>
<th>Country:</th>
</tr>
</table>
您可以像导入任何其他外部js文件一样导入js
<script src="js/myObject.js"></script>
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷
}
表,th,td{
文本对齐:左对齐;
填充:5px10px
}
#
客户名称:
国家:
遍历myObject.customers
创建适当的元素并将它们添加到DOM中
<script src="js/myObject.js"></script>