【问题标题】:Js:Calling a function on clickJs:点击时调用函数
【发布时间】:2014-11-04 22:15:11
【问题描述】:

我使用 Mapplace.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>

如何通过带键的链接调用?

【问题讨论】:

    标签: javascript jquery google-maps jquery-plugins


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

    • 我尝试添加 js 代码和链接,但由于某种原因这不起作用。 TypeError: e.prevetDefault 不是函数 e.prevetDefault();
    • 你好 RQEST,我弄错了,它叫 e.preventDefault();
    • 你好。感谢您的帮助,但我找到了另一种方法,我们不得不花费大量时间))。
    【解决方案2】:

    由于ViewOnMap是在Maplace原型上定义的,这意味着这是一个实例方法。所以你应该在 Mapplace 实例对象上调用它。像这样的:

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

    及以后:

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

    【讨论】:

    • 这个选项很遗憾我也不起作用。也许您有一个如何正确执行此操作的示例?
    • 你能贴出你的代码吗,你是如何创建 Maplace 对象的?
    • &lt;script&gt; var maplace = new Maplace({ locations: LocsA, map_div: '#gmap', controls_type: 'list', controls_on_map: true }).Load(); ; &lt;/script&gt;
    • 我认为应该是var maplace = new Maplace({...}); maplace.Load(); ... maplace.ViewOnMap(2)。查看更新的答案。
    • 如果我使用maplace.Load ();,则地图未加载。
    【解决方案3】:

    我是这样决定这个问题的:

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

    尝试了很多东西,一切都很简单。无需更改任何内容或附加到代码。 谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2016-09-17
      • 2012-11-24
      • 2016-04-24
      相关资源
      最近更新 更多