【问题标题】:I am trying to attach a hover action, but the hover action is being called immediately我正在尝试附加悬停动作,但正在立即调用悬停动作
【发布时间】:2012-12-01 20:06:16
【问题描述】:

我正在编写我的第一个 jQuery 插件。我正在尝试添加悬停动作,但会立即调用悬停响应。这是我的插件:

(function($){
  var Help = function(element, options) {
    this.$element = element;
    this.options = $.extend({}, $.fn.help.defaults, options);
  };

  Help.prototype.hover = function(name){
    alert(name);
  };

  $.fn.help = function(option){
    return this.each(function () {
      var $this = $(this);
      var help = $this.data('help');
      var options = "";
      var data = $this.data('help-id');
      if(!help) {
        $this.data('help', (help = new Help(this, options)));
      }
      $this.on('hover', help.hover(data));
    });
  };
})(jQuery);

我用$('#one').help() 附加了我的插件,但这会立即调用悬停函数。我究竟做错了什么?

【问题讨论】:

    标签: javascript jquery jquery-plugins jquery-events


    【解决方案1】:

    您不是将函数传递给on,而是函数调用的结果。

    替换

    $this.on('hover', help.hover(data));
    

    $this.on('hover', function(){help.hover(data)});
    

    【讨论】:

    • 完美。谢谢您的帮助。当我允许时会接受答案。 (8 分钟)
    猜你喜欢
    • 2021-08-29
    • 2014-09-22
    • 2016-08-15
    • 2014-06-02
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多