[英]Js:Calling a function on click
我使用Maplace.js,這是jQuery的Google Maps Javascript插件。 在一個插件中,有一個功能可以在地圖上顯示標記。
//triggers to show a location in map
Maplace.prototype.ViewOnMap = function (index) {
//view all
if (index === this.view_all_key) {
this.o.beforeViewAll();
this.current_index = index;
if (this.o.locations.length > 0 && this.o.generate_controls && this.current_control && this.current_control.activateCurrent) {
this.current_control.activateCurrent.apply(this, [index]);
}
this.oMap.fitBounds(this.oBounds);
this.CloseInfoWindow();
this.o.afterViewAll();
//specific location
} else {
index = parseInt(index, 10);
if (typeof (index - 0) === 'number' && index > 0 && index <= this.ln) {
try {
google.maps.event.trigger(this.markers[index - 1], 'click');
} catch (err) {
this.debug('ViewOnMap::trigger', err.stack);
}
}
}
return this;
};
我嘗試調用此方法,但無濟於事:
<a href="javascript:Maplace.prototype.ViewOnMap(2)">Link</a>
如何從帶有鑰匙的鏈接中撥打電話?
由於ViewOnMap
是在Maplace
原型上定義的,因此這意味着這是一個實例方法。 因此,您應該在Maplace實例對象上調用它。 像這樣:
var maplace = new Maplace({ ... });
maplace.Load();
然后:
<a href="javascript:maplace.ViewOnMap(2)">Link</a>
您可以這樣做:
<a class="openmap" href="#">Link</a>
和js:
$('.openmap').on('click', function(e){
e.prevetDefault();
Maplace.prototype.ViewOnMap = function (index) {
//view all
if (index === this.view_all_key) {
this.o.beforeViewAll();
this.current_index = index;
if (this.o.locations.length > 0 && this.o.generate_controls && this.current_control && this.current_control.activateCurrent) {
this.current_control.activateCurrent.apply(this, [index]);
}
this.oMap.fitBounds(this.oBounds);
this.CloseInfoWindow();
this.o.afterViewAll();
//specific location
} else {
index = parseInt(index, 10);
if (typeof (index - 0) === 'number' && index > 0 && index <= this.ln) {
try {
google.maps.event.trigger(this.markers[index - 1], 'click');
} catch (err) {
this.debug('ViewOnMap::trigger', err.stack);
}
}
}
return this;
};
});
我以這種方式決定了這個問題:
<a onclick="javascript:maplace.ViewOnMap()" href="#gmap">Link</a>
嘗試了很多事情,一切都很容易。 無需更改任何內容或附加到代碼。 謝謝你們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.