Javascript 调用Google Maps事件侦听器时,Mootools类属性未定义
我不熟悉mootools和Google Maps API V3,我面临以下问题: 我的目标是创建一个基类,用不同的“单击”事件处理程序来表示通用按钮和子类,以添加标记、多边形和多段线。 因此,我使用mootools创建了一个具有基本外观属性的基类和三个具有不同“click”事件处理程序的子类。 按钮显示正常,但当我单击其中一个按钮时,它表示属性未定义。这是一个范围问题吗?如何修改代码以调用正确的方法Javascript 调用Google Maps事件侦听器时,Mootools类属性未定义,javascript,google-maps,mootools,Javascript,Google Maps,Mootools,我不熟悉mootools和Google Maps API V3,我面临以下问题: 我的目标是创建一个基类,用不同的“单击”事件处理程序来表示通用按钮和子类,以添加标记、多边形和多段线。 因此,我使用mootools创建了一个具有基本外观属性的基类和三个具有不同“click”事件处理程序的子类。 按钮显示正常,但当我单击其中一个按钮时,它表示属性未定义。这是一个范围问题吗?如何修改代码以调用正确的方法 var GButton = new Class({ initialize: f
var GButton = new Class({
initialize: function(divObj, mapObj){
this.div = divObj;
this.map = mapObj;
this.controlUI = document.createElement('DIV');
this.controlUI.style.backgroundColor = 'white';
this.controlUI.style.borderStyle = 'solid';
this.div.appendChild(this.controlUI);
this.controlText = document.createElement('DIV');
this.controlText.style.fontFamily = 'Arial,sans-serif';
this.controlUI.appendChild(this.controlText);
}
});
//mootools 1.1
var GButtonMarker = GButton.extend({
initialize: function(divObj, mapObj){
this.parent(divObj, mapObj);
this.controlUI.title = 'Click to add new marker';
this.controlText.innerHTML = 'New Marker';
google.maps.event.addDomListener(this.controlUI, 'click', this.updateControl);
},
//just to represent that it is pressed
updateControl: function(){
this.controlUI.style.backgroundColor = 'LightGrey'; //problem here! this.controlUI UNDEFINED! (while it is defined of course...)
this.controlText.style.fontWeight = 'bold';
}
});
您需要将类实例绑定到回调:
google.maps.event.addDomListener(this.controlUI, 'click', this.updateControl.bind(this));
顺便说一句,这是一个mootools包装器,不是谷歌的(如果有的话)。你有没有尝试过
'click',this.updateControl.bind(this)
?这在V2中很容易解决,第三个参数是GEvent.bindDom
。看起来他们从V3的addDomListener
中删除了上述参数。有趣的是,他们把它放在了google.maps.event.bind(非DOM事件)的中。我刚刚做了,它可以工作了,谢谢。将它迁移到一个答案中,这样它就可以关闭了。