简体   繁体   English

Js:点击调用函数

[英]Js:Calling a function on click

I use Maplace.js, A Google Maps Javascript plugin for jQuery. 我使用Maplace.js,这是jQuery的Google Maps Javascript插件。 In a plugin has a function that will display the marker on the map. 在一个插件中,有一个功能可以在地图上显示标记。

//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;
    };

I try to call this method but to no avail: 我尝试调用此方法,但无济于事:

<a href="javascript:Maplace.prototype.ViewOnMap(2)">Link</a>

how can I call from a link with key? 如何从带有钥匙的链接中拨打电话?

Since ViewOnMap is defined on Maplace prototype, it means that this is an instance method. 由于ViewOnMap是在Maplace原型上定义的,因此这意味着这是一个实例方法。 So you should call it on Maplace instance object. 因此,您应该在Maplace实例对象上调用它。 Something like this: 像这样:

var maplace = new Maplace({ ... });
maplace.Load();

and later: 然后:

<a href="javascript:maplace.ViewOnMap(2)">Link</a>

You can do it like this: 您可以这样做:

<a class="openmap" href="#">Link</a>

and the js: 和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;
    };
});

I decided this question in this way: 我以这种方式决定了这个问题:

<a onclick="javascript:maplace.ViewOnMap()" href="#gmap">Link</a>

Tried many things, and everything was easy. 尝试了很多事情,一切都很容易。 No need to change anything, or append to the code. 无需更改任何内容或附加到代码。 Thank you all. 谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM