Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript调用数据文件,然后以表格形式显示_Javascript_Html - Fatal编程技术网

如何使用JavaScript调用数据文件,然后以表格形式显示

如何使用JavaScript调用数据文件,然后以表格形式显示,javascript,html,Javascript,Html,myObject.js var myObject= {"customers":[ {"CustomerName" : "Alfreds Futterkiste","Country" : "Germany"}, {"CustomerName" : "Berglunds snabbköp","Country" : "Sweden&q

myObject.js

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>