简体   繁体   English

Jquery。 单击链接时调用 function

[英]Jquery. Call a function when clicking on a link

Given: There is a JS function that calls the list of cities.给定:有一个调用城市列表的JS function。 Included in the header as a js file包含在header作为js文件

GeoIPModule.prototype.loadCities = function(callback) {
        var self = this;
        if (!this.citiesLoaded) {
            $.ajax({
                url:      self.http_host + 'index.php?route=extension/module/geoip/getList',
                dataType: 'html',
                success:  function(html) {
                    self.chooseBlock.html(html);
                    var input = self.chooseBlock.find('.geoip-popup-input');
                    self.autocomplete(input, self.chooseBlock.find('.geoip-body'));
                    input.siblings('ul.dropdown-menu').css({'maxHeight': 300, 'overflowY': 'auto', 'overflowX': 'hidden'});
                    input.focus();
                    self.citiesLoaded = true;
                    callback.apply();
                }
            });
        }
    };

Task: It is necessary to call the function, ie call up a list of cities by clicking on the link in any part of the document任务:需要调用function,即点击文档任意部分的链接调出城市列表

Trying to do this:尝试这样做:

<a href="javascript:void(0);" id="load-сities">ССЫЛКА</a>

<script type="text/javascript">
  $("#load-сities").click(function(e) {
    GeoIPModule.prototype.loadCities();
  });
</script>

But I get the error: ReferenceError: GeoIPModule is not defined但我收到错误: ReferenceError: GeoIPModule 未定义

Complete script https://jsfiddle.net/3L604e7m完整脚本https://jsfiddle.net/3L604e7m

If you want GeoIPModule to be globally available, you can set the property on the window .如果您希望GeoIPModule全局可用,您可以在window上设置属性。 Currently, it is only visible inside the immediately-invoked function expression.目前,它仅在立即调用的 function 表达式中可见。

window.GeoIPModule = function(o, el){
  //...
}

JSFiddle JSFiddle

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

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