如何在此网址中添加 lat 和 lon 变量:
<a class="geo" href="http://dev.site.com/geolocation?distance[latitude]=LAT&distance[longitude]=LON&distance[search_distance]=20&distance[search_units]=km">GPS</a>
javascript from:http://jsfiddle.net/yJrtR/14/
window.onload = function () {
var latElement = document.getElementById("lat"),
lonElement = document.getElementById("lon"),
lastUpdatedElement = document.getElementById("last_updated"),
watchPositionOptions = {
enableHighAccuracy: false,
timeout: 10000,
maximumAge: 0
};
navigator.geolocation.watchPosition(function (position) {
console.log("Successfully retrieved position: ", position);
var coords = position.coords;
latElement.innerHTML = coords.latitude;
lonElement.innerHTML = coords.longitude;
lastUpdatedElement.innerHTML = new Date(position.timestamp);
}, function (error) {
console.log("Something went wrong retrieving position: ", error);
}, watchPositionOptions);
};
答案 0 :(得分:0)
使用连接字符串构造URL,例如:
var URL = "http://dev.site.com/geolocation?";
URL =+ "distance[latitude]=" + LAT;
URL =+ "&distance[longitude]=" + LON;
URL =+ "&distance[search_distance]=20&distance[search_units]=km";
然后使用jQuery将href=
设置为URL
$("a.target").attr("href", URL);
或通过修改javascript中的属性。