<!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>Google Maps JavaScript API v3 Example: Reverse Geocoding</title>
<link
href="https://developers.google.com/maps/documentation/javascript/examples/default.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var variablejs;
geocoder = new google.maps.Geocoder();
var input = "41.021355,-96.020508";
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
variablejs = results[0].formatted_address; * * //document.write(variablejs); Not working**
*
//window.location.href = "http://localhost/test2.php?ad="+ variablejs; Working*
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
}); * * document.write(variablejs); * * //writes undefined variable (not working)
</script>
</head>
<body></body>
</html>
的部分(//document.write(variablejs);不工作),不会写变量 “variablejs”,但是当我使用的方法(// window.location.href =“http://localhost/test2.php?ad =”+ variablejs;工作),它会写入url并转发传递变量的页面作为参数。我想要做的是在同一个HTML的主体中打印该变量。 我试图首先初始化变量,然后写入正文,但它看起来像该部分是不可访问像全局变量或类似的东西。请帮忙。写JavaScript变量
change var variablejs; to var variablejs =“”;你应该很好去 – Satya 2012-04-19 07:09:38
为什么不把这样的东西放进去:
,并将document.write(something)改为document.getElementById('output')。innerHTML = something ;? – 2012-04-19 07:23:04