我正在尝试使用jQuery,但ready
函数似乎不起作用:
<html>
<head></head>
<body>
<div id="locationdemo">
<p id="demo">Click the button to get your coordinates:</p>
<button id="locationButton" onclick="getLocation()">Click me!</button>
</div>
<div id="loading" clas="load">
<p>Acquiring Location</p>
<img border="0" src="ajax-loader.gif" alt="Loading" width="32" height="16">
</div>
脚本从这里开始;
<script src="Distance.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
alert("why isn't this running?");
$("#loading").hide();
});
var x=document.getElementById("demo");
var retries = 0;
function getLocation() {
alert("before check");
if (navigator.geolocation) {
var timeoutVal = 6000;//in milliseconds
var watchid = navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
} else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function displayPosition(position) {
var accuracy = position.coords.accuracy;
var maxAcceptableAccuracy = 300.48//in meters, this is eqaul to 100 feet
alert("accuracy: "+accuracy);
if(accuracy > maxAcceptableAccuracy && retries < 10) {
retries = retries++;
getLocation();
return;
}
retires = 0;
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function displayError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
</script>
</body>
</html>
警报(“为什么不运行”);没有运行。我究竟做错了什么?我已经坚持了2个小时。
答案 0 :(得分:2)
我编辑了位置以指向正确的CDN并在加载jQuery后导入distance.js脚本文件
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="Distance.js"></script>
<script>
$(document).ready(function(){
alert("why isn't this running?");
$("#loading").hide();
});
var x=document.getElementById("demo");
var retries = 0;
function getLocation()
{
alert("before check");
if (navigator.geolocation)
{
var timeoutVal = 6000;//in milliseconds
var watchid = navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function displayPosition(position)
{
var accuracy = position.coords.accuracy;
var maxAcceptableAccuracy = 300.48//in meters, this is eqaul to 100 feet
alert("accuracy: "+accuracy);
if(accuracy > maxAcceptableAccuracy && retries < 10)
{
retries = retries++;
getLocation();
return;
}
retires = 0;
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function displayError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
</script>
答案 1 :(得分:1)
试试这个:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert("why isn't this running?");
$("#loading").hide();
});
var x=document.getElementById("demo");
var retries = 0;
function getLocation()
{
alert("before check");
if (navigator.geolocation)
{
var timeoutVal = 6000;//in milliseconds
var watchid = navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function displayPosition(position)
{
var accuracy = position.coords.accuracy;
var maxAcceptableAccuracy = 300.48//in meters, this is eqaul to 100 feet
alert("accuracy: "+accuracy);
if(accuracy > maxAcceptableAccuracy && retries < 10)
{
retries = retries++;
getLocation();
return;
}
retires = 0;
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function displayError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
</script>
</body>
</html>