如何在html代码中输入javascript变量

How to input javascript variables in to html code?

本文关键字:输入 javascript 变量 代码 html      更新时间:2023-09-26

我正试图为一个应用程序创建一个里程计算器,我在网上找到了代码。

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var directionDisplay;
var map;

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
    zoom:12,
   `enter code here` mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: copenhagen
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}

var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
    }
});
}
</script>
<title>Distance Calculator</title>
<style type="text/css">
        body {
            font-family:Helvetica, Arial;
        }
        #map_canvas {
            height: 50%;
        }
</style>
</head>
<body>
<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
    <div>
        <p>
            <label for="start">Start: </label>
            <input type="text" name="start" id="start" />
            <label for="end">End: </label>
            <input type="text" name="end" id="end" />
            <input type="submit" value="Calculate Route" onclick="calcRoute()" />
        </p>
        <p>
            <label for="distance">Distance (km): </label>
            <input type="text" name="distance" id="distance" readonly="true" />
        </p>
    </div>
    <div id="map_canvas"></div>
</body>
</html>
javascript google geolocation maps 

我希望页面上的链接上有一个点击电子邮件链接,打开用户的电子邮件(如电脑上的Windows Live Mail或平板电脑上的雅虎邮件应用程序),并在电子邮件正文中说"你的里程数是??"。我可以这样做,但我希望里程数能自动输入。我在脚本的html部分添加了这段代码。。。

<p><a href="mailto:youremail.yahoo.com?subject=Mileage
&body=calcRoute('string', 'distance')">Click to send email</a></p>

它打开了电子邮件,但没有显示计算出的里程数。相反,在正文中显示calcRoute("环,距离")。如何获取它来显示里程数?

我知道这个问题很长,但如果你能抽出时间回答,我将不胜感激。尽快得到一个有效的答案是非常重要的。非常感谢。

您必须计算链接onclick事件中的mailto属性值。假设calcRoute函数返回一个字符串,那么以下代码应该可以工作:

<a href="mailto:youremail.yahoo.com?subject=Mileage&body="
   onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a>

你应该看看这个链接应该对有用

  • http://www.webdevelopersnotes.com/tips/html/html_mailto_attribute_html_tips.php3

  • 如何使用Mailto URL发送带有主题的邮件?

我的猜测是它没有探测到主题。您确定您的javascript CalcRoute正在返回一个变量吗?