我有一個表,有兩行,每行有兩個單元格,每個單元格有幾個信息,如辦公室名稱,地址和電話號碼等。每個jQuery使用jquery,地址從每個單元格中拉出並饋送進入谷歌地圖地理編碼器對象以獲取該點並將其繪製在地圖中。現在,在地址值的每次命中時,我還想從jquery獲取地址值的當前單元格中獲取手機和辦公室名稱的唯一值。我需要這些值,以便我可以在InfoWindow中顯示這些值地圖?我如何獲得這些值?如何使用jQuery獲取父元素值?
<table class="OfficeInfo" border="0" style="width: 100%" cellspacing="10px" cellpadding="15px">
<tr>
<td class="Office1" style="width=40%">
<span class="OfficeName">
<div id="ctl00_PlaceHolderMain_ctl00_ctl17_label" style='display:none'>Office1Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl17__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl17_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Francis Hospital</a></div></div>
</span>
<span class="Address">
2001 86th Street West <br />Indianapolis, IN 46260
</span> <br />
<span class="Phone">
(402) 123-1234</span><br /><br />
<a class="mapdirectionsLink" href="#">map & directions></a><br /><br />
<span class="Hours">
MTW:9:00AM-5:00PM</span>
</td>
<td class="Office2" style="width:40%">
<span class="OfficeName">
<div id="ctl00_PlaceHolderMain_ctl00_ctl21_label" style='display:none'>Office2Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl21__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl21_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Margaret's Hospital</a></div></div>
</span>
<span class="Address">
8075 North Shadeland Avenue <br />Indianapolis, IN 46250</span><br />
<span class="Phone">
(316) 123-3245</span><br /><br />
<a class="mapdirectionsLink" href="#">map & directions></a><br /><br />
<span class="Hours">
MTW:9:00AM-5:00PM</span>
</td>
</tr>
<tr>
<td class="Office3" style="border-top:1px dotted silver; width:40%;">
<span class="OfficeName">
<div id="ctl00_PlaceHolderMain_ctl00_ctl25_label" style='display:none'>Office3Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl25__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl25_label"><div class="ms-rtestate-field"><a href="/" target="_blank">Munster Women's Center</a></div></div>
</span>
<span class="Address">
395 Westfield Road <br />Noblesville, IN 46060</span><br />
<span class="Phone">
(316) 123-3245</span><br /><br />
<a class="mapdirectionsLink" href="#">map & directions></a><br /><br />
<span class="Hours">
MTW:9:00AM-5:00PM</span>
</td>
<td style="border-top:1px dotted silver; width:40%">
</td>
</tr>
</table>
function codeAddress() {
var infowindow = new google.maps.InfoWindow({});
$('span.Address').each(function(index) {
var addy = $(this).text();
var off_name = $(this).siblings('.OfficeName').children(.ms-rtestate-field).text();
var infowindow = new google.maps.InfoWindow({ content: 'Hello world' });
geocoder.geocode({ 'address': addy}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title:addy
});
//var currentCell=(this).closest(td).html());
// Adding a click event to the marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<span style="color:#808080; font-size:13px; font-family:Trebuchet">'+addy
+'<br> <a href="http://maps.google.com/maps?saddr=&daddr=' + this.position.toUrlValue() + '" target ="_blank">Get Direction To Here<\/a>'+
off_name + '</span>');
infowindow.open(map, this);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
}
可能是聰明也加入您的JavaScript,或者更好的是剛剛貼上的相關部分,其餘部分添加到的jsfiddle(HTTP:// jsfiddle.net) – Arend
我現在也添加了JavaScript ...我想獲得當前上下文的'this'的辦公室名稱和電話號碼.. – AJSwift
不是我想挑剔的細節,我建議您識別您的具有唯一ID的元素,而不是類。這樣你可以針對特定的元素。儘管可以通過類來實現,但一個類的目的是作爲一個通用的聲明,它可以被許多元素共享,而一個id對於單個元素是特定的和唯一的。 – stefgosselin