解决方案:
使用 $('#map_canvas').gmap() 上的 click 事件监听器作为一个包罗万象的方法,使用 mainevent 函数来确定被点击对象的类名,然后使用 if 语句查看它是否与我们希望在 infoWindow 中使用的任何“选择器”匹配,并从那里执行常规功能。
为方便起见,jQuery(element) 被指定为与普通的 jQuery(this) 等效
类被拆分为一个数组,以说明单击对象上的多个类。
然后我们只需使用 if 语句来检查我们希望用作选择器的类是否在被单击对象的类名数组中,如果是,则放入我们通常会放入的 jQuery 中选择器的嵌套单击功能,只需在必要时将 (element) 替换为 (this)。
这可以很容易地适应使用 id 作为选择器而不是类 - 只需将 $(element).attr('class') 更改为 $(element).attr('id')
这是示例代码:
jQuery('#map_canvas').gmap().addEventListener('click', function(mainevent) {
// We cannot directly target elements inside the Google map and infoWindows with jQuery, so here we catch all clicks on the map and then use if statements to see if the clicked element matches any of the selectors we wish to use.
var element = jQuery(mainevent.target);
if ( $(element).attr('class') ) { var MainEventClassList = $(element).attr('class').split(' '); }
// Gets the classes of the clicked element into an array, so we can check for matches with if statements, as an equivalent to typical jQuery class selectors. We have also made (element) available as a replacement for (this).
if ( jQuery.inArray( 'SelectorClassName', MainEventClassList ) >= 0 ) {
mainevent.preventDefault();
mainevent.stopPropagation();
var destinationid = jQuery(element).attr('href');
alert(destinationid);
}
});