jQuery 自定义事件
为什么这个没有被触发?
$('#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可以订阅事件
Why isn't this being triggered?
$('#nav').bind('app:event', function (event, status) {
console.log([event, status]);
});
when this is:
$(document).bind('app:event', function (event, status) {
console.log([event, status]);
});
the event is being fired using:
$(this).trigger('app:event', [options]);
from within a plugin.
This is the entire block:
/* 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));
I'm trying to implement something like a suplish/subscribe/observer like solution where a #id can subscribe to events
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试放置
$(document).ready()
。Try to put
$(document).ready()
around.代码这么少很难说。当您尝试触发它时,您确定 $(this) 引用 $("#nav") 吗?
Difficult to say with so little of the code. Are you sure $(this) refers to $("#nav") when you try to trigger it?