javascript - js :Calling a function on click

标签 javascript jquery google-maps jquery-plugins

我使用 Maplace.js,一个用于 jQuery 的 Google map Javascript 插件。 插件中有一个功能可以在 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;
    };

我尝试调用此方法但无济于事:

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

如何通过带有 key 的链接进行调用?

最佳答案

你可以这样做:

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

关于javascript - js :Calling a function on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26746089/

相关文章:

android - Android 中的谷歌地图 : Failed to find style 'mapViewStyle' in current theme

javascript - 当网页未最小化或打开新选项卡时,在 JavaScript 超时之前清除计时器

javascript - 如何从异步调用返回响应?

javascript - 使用 ng-repeat 时无法添加和删除类

jquery - 删除正则表达式 .replace 中的最后一个字符

javascript - 为什么这两种编写 jQuery 插件的方式是等价的?

javascript - Webkit 似乎有百分比宽度/高度的问题

javascript - Google map javascript Api 无法在本地主机上运行

javascript - 从 JSON 文件中获取特定信息

javascript - php 中的 POST 和提交按钮问题