0

为什么不触发这个?

$('#nav').bind('app:event', function (event, status) {
  console.log([event, status]);
});

当这是:

$(document).bind('app:event', function (event, status) {
  console.log([event, status]);
});

使用以下方式触发事件:

$(this).trigger('app:event', [options]);

从插件中。

这是整个区块:

/* App.js */
$(function ($) {

  // UI Panels
  $('#panels').panels({
    controls: '#nav a'
  });

  $('#nav').bind('app:event', function (event, status) {
    console.log([event, status]);
  });

});

/**
 * Plugin dir
 */
(function($) {
  // Interface
  $.fn.panels = function (options) {
    var settings = {}, current;

    if (options) {
      $.extend(settings, options);
    }

    // Setup
    $('.panel', this).hide();
    $('.panel:first', this).slideDown('slow');

    return this.each(function () {
      var self = $(this);

      $(settings.controls).click(function () {
        // Get reference
        current = this.href.substring(this.href.indexOf('#') + 1);

        // Hide all
        $('.panel', self).hide();
        // Show current
        $('#'+ current)
          .publish({            
            'panelReady': current
          })
          .slideDown();

        return false;
      });
    });
  };

  // Publish event
  $.fn.publish = function (options) {    
    var settings = {
      origin: this      
    };

    if (options) {
      $.extend(settings, options);
    }

    return this.each(function () {
      $(this).trigger('app:event', [options]);
    });
  };

}(jQuery));

我正在尝试实现类似 suplish/subscribe/observer 之类的解决方案,其中 #id 可以订阅事件

4

2 回答 2

0

试着放一放$(document).ready()

$(document).ready(function() {
    $('#nav').bind('app:event', function (event, status) {
          console.log([event, status]);
    });
});
于 2009-10-14T13:49:15.057 回答
0

这么少的代码很难说。当您尝试触发它时,您确定 $(this) 指的是 $("#nav") 吗?

于 2009-10-14T13:49:27.153 回答