Google지도가 잘 작동하는 웹 페이지가 있습니다. 대신이 "보훔"하드 코드 도시 이름을 가진, 나는 헤더를Javascript : id가 "map"인 h3 헤더의 내용을 찾는 방법은 무엇입니까?
<h3 id="city"><i>Bochum</i></h3>
를 찾아 내 초기화() 함수에서 그 값을 사용하고 싶습니다.
나는 아래 코드에서 사소한 것이 빠졌을 것입니다. 제발 저를 도우며 자바 API 기술은 매우 녹슬다.
또한 내 마커의 색상처럼 h3 헤더를 통해 한 가지 더 많은 가치를 전달할 수 있을지 궁금합니다.
감사합니다. 알렉스
<html>
<head>
<style type="text/css">
h1,h2,h3,p { text-align: center; }
#map { width: 400; height: 200; margin-left: auto; margin-right: auto; }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function init() {
for (var child in document.body.childNodes) {
child = document.body.childNodes[child];
if (child.nodeName == "H3")
alert(child);
}
// use the #city value here instead
city = 'Bochum';
if (city.length > 1)
findCity(city);
}
function createMap(center) {
var opts = {
zoom: 9,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
return new google.maps.Map(document.getElementById("map"), opts);
}
function findCity(city) {
var gc = new google.maps.Geocoder();
gc.geocode({ "address": city}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var pos = results[0].geometry.location;
var map = createMap(pos);
var marker = new google.maps.Marker({
map: map,
title: city,
position: pos,
});
}
});
}
</script>
<head>
<body onload="init();">
<h3 id="city"><i>Bochum</i></h3>
<div id="map"></div>
</body>
</html>
처음에는 정말로 jQuery를 사용해야합니다. http://jquery.com/ – CarneyCode
한 줄로 작업을 수행하기 위해 80kb 라이브러리를로드하는 것이 왜 필요합니까? –
@ Dr.Molle : 압축 한 번만 26kb입니다. 응용 프로그램 코드를 더 간단하고 안정적인 것으로 만드는 것과 전체 페이지 크기를 더 작게 만드는 것 사이의 절충안입니다. 예를 들어 $ ('# city'). text()를 사용하면 태그가 제거되면 더 짧아지고 끊어지지 않습니다. – Douglas