1

如何 在此 url 中 添加latlon变量:

<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: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);
};
4

1 回答 1

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";

然后href=URLjQuery 设置

 $("a.target").attr("href", URL);

或者通过修改javascript中的属性。

于 2013-10-29T19:56:16.273 回答