jQuery插件回调函数参数 [英] jQuery plugin callback function parameters

查看:78
本文介绍了jQuery插件回调函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更好的方式来访问/管理插件回调函数中的数据。
我想做与jQuery UI相同的事情。

I am looking for a better way to access/manage data inside a plugin callback function. I want to do the same thing as the jQuery UI.

UI示例:(我想怎么做) http://api.jqueryui.com/sortable/

UI example:(how I want to do this) http://api.jqueryui.com/sortable/

$( ".selector" ).sortable({
   activate: function( event, ui ) {
     alert(ui.item)
     alert(ui.position)
     alert(ui.offset)
   }
});

我的插件示例(我现在如何拥有):

my plugin example(how I now have it):

$( ".selector" ).myplugin({
   activate: function( event, item, postion, offset ) {//to much parameters
     alert(item)
     alert(position)
     alert(offset)
   }
});

//inside the plugin
var varItem = '';
var varPosition = '';
var varOffset = '';

if(typeof self.o.activate == 'function'){
    self.o.activate.call(this, varItem, varPosition, varOffset);
}


推荐答案

这应该可以解决问题

$( ".selector" ).myplugin({
   activate: function( event, object ) {//to much parameters
     alert(object.item)
     alert(object.position)
     alert(object.offset)
   }
});

//inside the plugin
var varItem = '';
var varPosition = '';
var varOffset = '';

if(typeof self.o.activate == 'function'){
    self.o.activate.call(this, {item: varItem, position: varPosition, offset: varOffset});
}

这篇关于jQuery插件回调函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆