0
觸發內部功能跳到底部問題jQuery插件 - 由回調
jQuery插件:
$.fn.myPlugin = function(options) {
var options = $.extend({
myOption: true,
edit: function() {},
done: function() {}
}, options);
options.edit.call(this);
options.done.call(this);
//plugin guts removed to prevent over complication
return {
edit: function(obj) {
$(obj).closest('#myParent').find('#myInput').autosizeInput(); //plugin to autosize an input
},
done: function(obj) {
$(this).closest('tr').find('td').not('.not').each(function(i) {
//do some things
});
}
}
});
請記住這是我的一個插件的削減版本。
從頁面調用:
$(document).ready(function() {
var myPlugin = $('.editable').myPlugin({
edit: $(this).on('click', '.edit-td', function(e) {
e.preventDefault();
//do some page specific stuff
myPlugin.edit($(this)); //call the edit returned function
}),
done: $(this).on('click', '.done-td', function(e) {
e.preventDefault();
//do some page specific stuff
myPlugin.done($(this)); //call the done returned function
});
});
});
這個偉大的工程在大多數情況下,不過,我真正想要的是有功能的叫我的插件內的每一個具體的觸發回調時間 - 沒有需要從外部調用插件。
我有試過,包括在我的插件委派事件:
$(this).on('click', '.edit-td', function(e) {
e.preventDefault();
$(this).closest('#myParent').find('#myInput').autosizeInput();
});
$(this).on('click', '.done-td', function(e) {
e.preventDefault();
$(this).closest('tr').find('td').not('.not').each(function(i) {
//do some things
});
});
但當.edit-td
被觸發,傳播和觸發.done-td
情況下,如果我在edit-td
功能把e.stopPropagation()
(因爲它已經被委託) edit-td
完全停止發射。
和非委託的方法:
$(this).find('.done-td').click(function(e, this) {});
但內部功能完成之前,我不能返回的對象(this
)解析到內部功能。 (剛出現未定義或missing formal parameter
)。
*跳到這裏
爲了避免這一問題成爲局部 -
我需要有內部功能從打電話給我 插件,在一個特定觸發回調的時間。 如果不調用它使用閉
喜歡的東西:
if($.fn.myPlugin.callback().is('edit')) {
//fire function
}
http s://learn.jquery.com/plugins/advanced-plugin-concepts/ – Rashi 2015-06-02 10:07:04
這是一個有趣的閱讀,實際上是解決了我的問題。請提供這個答案,我會接受。維布勒。 – Edward 2015-06-02 10:49:01
當我讀到這個問題時,我覺得你沒有得到關於回調的想法,這就是爲什麼我評論這個鏈接。如果你有答案,你可以關閉這個問題或者保留它以獲得更好的答案。我對你想要完成的事情沒有多少了解。 – Rashi 2015-06-02 17:37:02