1 回答
TA贡献1155条经验 获得超0个赞
最简单的解决方案是将 from 更改为id,class因为同一页面上只有一个元素可以具有相同的 ID。然后将 jQuery 选择器从 更改$('#state')为$('.state')因为我们现在正在使用类。
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Geo City Locator</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<body>
<div>State: <span class="state"></span></div>
<div>State: <span class="state"></span></div>
<div>State: <span class="state"></span></div>
<div>State: <span class="state"></span></div>
<div>City: <span id="city"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP: <span id="ip"></span></div>
<script>
$.getJSON('https://geolocation-db.com/json/')
.done (function(location) {
$('#country').html(location.country_name);
$('.state').html(location.state);
$('#city').html(location.city);
$('#postal').html(location.postal);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
});
</script>
</body>
</html>
添加回答
举报