如何使用脚本加载两个 json 文件,而不用一个脚本覆盖另一个?

How can I load two json files with script, without one script overwriting the other?

我有一个 html 文件,其中显示了两个制表符 table。 Table 1 连接到 saveFile1.json,table 2 连接到 saveFile2.json。我的问题是,saveFile2.json 中的 <script> 覆盖了 saveFile1.json 中的 <script>,因此两个 table 都显示了 saveFile2.json 中的内容。我怎样才能解决这个问题?谢谢。

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="MyCSS.css" >
        <script type="text/javascript" src="node_modules/tabulator-tables/dist/js/tabulator.js"></script>
    </head>
    
<body>

<table>
    <td>
        <div id="table1"></div>
        <script type="text/javascript" src="saveFile1.json"></script>
    </td>
    <td>
        <div id="table2"></div>
        <script type="text/javascript" src="saveFile2.json"></script>
    </td>
</table>

<script type="text/javascript" src="tabulatorTables.js"></script>

</body>
</html>

我猜(因为你没有提供 jsfiddle)你的 json 文件包含

saveFile1.json : var data = [ ... ];
saveFile2.json : var data = [ ... ];

并且您在每个制表符上使用相同的数据变量。

因此,最后一个 saveFile*.json 加载,获胜。

使用不同的变量名称(data1 和 data2)并将它们具体应用于每个制表符(table1.setData(data1); table2.setData(data2))。