gpt4 book ai didi

javascript - 如何将值从 Mustache 传递到 JavaScript?

转载 作者:行者123 更新时间:2023-11-28 00:22:54 25 4
gpt4 key购买 nike

生成 HTML 时,我只需调用

即可轻松从 JSON 对象中获取所需的属性
{{cmpyJson[jobCmpyID].company_name}}

那么 html 将包含“google”、“amazon”或任何其他要求的对象。

但是,当我尝试这样做

<script>
var company = cmpyJson[jobCmpyID].company_name
</script>

公司被分配null

那么我该如何将公司值设置为可通过 html 获取的字符串呢?

这是我尝试生成的整个页面:

<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var address = "lamar ";
//address = "austin, tx";
//var address ={{cmpyJson[jobCmpyID].company_name}}
//address +=" Company, ";
address += locJson[jobLocID].location_name;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 11,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);

var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});

var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});

} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:200px;"></div>
</body>

最佳答案

我认为将公司名称放入 HTML 中,然后在 JS 中从 HTML 中解析它是有意义的。示例:

<script>
// …
var address = document.getElementyById('company-name');
// …
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:200px;">
This should be a map showing the location of <span id="company-name">{{cmpyJson[jobCmpyID].company_name}}</span>.
</div>
</body>

这样,本地图加载失败时,您还可以得到一个很好的后备。

关于javascript - 如何将值从 Mustache 传递到 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29811613/

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