我已经复制了JavaScript google Maps,并且可以完美运行,但是我需要获取html控件的类型并将其设置为JavaScript。当我试图从lblTitle获取价值时,它不起作用。
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Geocoding Demo 1</title>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
//var address = 'Warsaw, PL'; Original var
var address = document.getElementById('lblTitle').innerText;
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 15
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
}
});
</script>
innerText是检索文本内容的方法之一,并且与大多数浏览器都不支持的大多数方法一样。如果我是正确的,则Firefox不支持innerText,因此,如果您正在Firefox中进行测试,则可能是无法读取该值的原因。
您应该做的是使用JQuery的text()方法获取值,因为它具有最大的跨浏览器支持,因此您无需找出哪个浏览器使用哪个属性。因此,在您的示例中:
var address = $("#lblTitle").text();
如果仍然无法显示文字,请在浏览器中检查html,以查看ID是否为lblTitle。如果您的服务器控件的标签类型为lblTitle,如下所示:
<asp:Label ID="lblTitle" runat="server" />
那么您的客户端ID不会是lblTitle,但会带有前缀。可以使用属性ClientID来请求客户端上的实际ID。因此,您在javascript中的代码行应为:
var address = $("#<%= lblTitle.ClientID %>").text();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句