gpt4 book ai didi

javascript - 输出距离矩阵结果为 div

转载 作者:行者123 更新时间:2023-12-03 09:37:24 25 4
gpt4 key购买 nike

我正在使用找到的代码 here计算地点之间的距离。

这是fiddle和代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
origin = new google.maps.LatLng(40.689844,-74.044874),
destination = "Washington, DC",
service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
},
callback
);

function callback(response, status) {
orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");

if(status=="OK") {
orig.value = response.destinationAddresses[0];
dest.value = response.originAddresses[0];
dist.value = response.rows[0].elements[0].distance.text;
} else {
alert("Error: " + status);
}
}
</script>
</head>
<body>
<br>
Basic example for using the Distance Matrix.<br><br>
Origin: <input id="orig" type="text" style="width:35em"><br>
Destination: <input id="dest" type="text" style="width:35em"><br>
Distance from Statue of Liberty to Washington DC, USA (by car): <input id="dist" type="text" style="width:35em"><br>
</body>
</html>

一切正常,但我想用 div 标签更改输入标签。但是,如果我将“input id”更改为“div id”,则 doesn't work :

Origin: <div id="orig" type="text" style="width:35em"></div>
Destination: <div id="dest" type="text" style="width:35em"></div>
Distance from Statue of Liberty to Washington DC, USA (by car): <div id="dist" type="text" style="width:35em"></div>

如果它是一个 div、input、span 或其他什么,只要它有那个 id,难道不应该是无关紧要的吗?应该更改什么才能将结果输出到 div 中?谢谢。

最佳答案

一个<div>没有value仅属性(property)input fields 。您可以将其设置为 innerHTML textContent 属性。

orig.innerHTML = response.destinationAddresses[0];

关于javascript - 输出距离矩阵结果为 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31292271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com