在 JQuery 事件处理程序中调用原型对象方法
Call Prototype Object Method in JQuery Event Handler
我有一个名为 Breadcrumb
的对象,它有一个函数 expandEllipses
。以下工作正常:
mbc = jQuery('#breadcrumb').breadcrumb({
items : initItems
});
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
mbc.expandEllipses();
});
但是,我想知道如何在我没有对象的 jQuery 事件处理程序中执行此操作:
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
parObj = <parent of the .breadcrumbEllipses instance that triggered this event>
protoObj = <Prototype object from parentObj>
protoObj.expandEllipses();
});
这就是 Barmar's 答案的结果:
jQuery.fn.breadcrumb = function(options) {
var bc = new Breadcrumb(this, options);
this.data('breadcrumb', bc);
return bc;
};
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
bcObj = jQuery(this).closest(".breadcrumb").data("breadcrumb");
bcObj.expandEllipses();
});
在 breadcrumb
初始化器中,使用 .data()
将 Breadcrumb
对象保存在元素中
this.data('breadcrumb', self);
那么你可以稍后检索它:
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
parObj = $(this).closest(".breadcrumb");
protoObj = parObj.data("breadcrumb");
protoObj.expandEllipses();
});
我有一个名为 Breadcrumb
的对象,它有一个函数 expandEllipses
。以下工作正常:
mbc = jQuery('#breadcrumb').breadcrumb({
items : initItems
});
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
mbc.expandEllipses();
});
但是,我想知道如何在我没有对象的 jQuery 事件处理程序中执行此操作:
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
parObj = <parent of the .breadcrumbEllipses instance that triggered this event>
protoObj = <Prototype object from parentObj>
protoObj.expandEllipses();
});
这就是 Barmar's 答案的结果:
jQuery.fn.breadcrumb = function(options) {
var bc = new Breadcrumb(this, options);
this.data('breadcrumb', bc);
return bc;
};
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
bcObj = jQuery(this).closest(".breadcrumb").data("breadcrumb");
bcObj.expandEllipses();
});
在 breadcrumb
初始化器中,使用 .data()
Breadcrumb
对象保存在元素中
this.data('breadcrumb', self);
那么你可以稍后检索它:
jQuery("body").on("click", ".breadcrumb .breadcrumbEllipses", function(e) {
parObj = $(this).closest(".breadcrumb");
protoObj = parObj.data("breadcrumb");
protoObj.expandEllipses();
});