我試圖找出爲什麼第一代碼工作,而第二個則沒有。我對jQuery和Javascript整體上都很滿意,但是在這個「$('#location')。html(...)」部分用'location'id填充了元素。如果我創建了一個變量的方式來該請求的結果被指定,它會做同樣的工作,如果我有「$(‘#位置’)。HTML(可變)」。是什麼賦予了?爲什麼我的修改當前位置請求代碼(JS/jQuery的)工作?
這裏有兩個代碼:
第一個代碼(這工作)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Current Position</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body >
<div>Location: <span id="location"></span></div>
<script>
$.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?')
.done (function(location) {
$('#location').html(location.city + ", " + location.state + " (" + location.country_name + ")");
});
</script>
</body>
</html>
第二個代碼(這其中不)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Current Position</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body >
<div>Location: <span id="location"></span></div>
<script>
var currentLocation = $.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?')
.done (function(location) {
location.city + ", " + location.state + " (" + location.country_name + ")";
});
$('#location').html(currentLocation);
</script>
</body>
</html>
謝謝你的鏈接。 –
不客氣。 –