gpt4 book ai didi

javascript - 外部 javascript 函数不工作

转载 作者:行者123 更新时间:2023-12-03 05:48:42 25 4
gpt4 key购买 nike

我试图在我的 html 文件中加载 map1() 函数,但它没有加载

这是 html。如果我把它放在一个 html 文件下,它可以正常工作,但现在我有一个单独的 html 和 js 文件,它就无法工作了。

<html>
<head>
<script src="map.js"></script>
</head>

<body>
<div id="map">

</div>
<script>
map1();
</script>
</body >

js是这样的。

function map1() {

var locations = [
['Rye Hall', 53.384915, -6.598975 ],
['Auxilla',53.384168, -6.598228 ],
['Education House', 53.383348, -6.597810 ],


['Gym', 53.384759, -6.603737 ],
['Students Union', 53.383056, -6.603694 ],
['Callan Building',53.383156, -6.602664 ],
['The Pheonix', 53.384368, -6.603641 ],
['Engineering Building', 53.384437, -6.602444, ],
['John Hume Building',53.383976, -6.600213, 4],
['Arts block', 53.383739, -6.601444, 5],
['Iontas', 53.384510, -6.600624, 3],
['Science Building',53.383125, -6.600575 , 2],
['Eoals', 53.384616, -6.601662, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: new google.maps.LatLng(53.381567, -6.599668),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}

最佳答案

您似乎在尚未完全加载时调用了 map1() 函数。如果您在调用 map1 之前等待文档加载,则不会发生这种情况。

<html>
<head>
<script src="map.js"></script>
</head>

<body>
<div id="map">

</div>
<script>
window.onload = function() {
map1();
}
</script>
</body >

关于javascript - 外部 javascript 函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40236778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com