创建后与jQuery插件对象交互

ICR*_*ICR 2 javascript jquery

我有一个自动收报机,使用轮询更新项目.我为自动收录器编写了一个简单的jQuery插件,它被调用如下:

$("#cont ul").ticker();
Run Code Online (Sandbox Code Playgroud)

这会将ul变成一个自动收报机,滚动浏览li.要添加新项目,我必须将lis添加到ul,这可以正常工作.但是,我的OO希望我可以在ticker对象上有一个addItem函数.但是,我不想失去jQuery使用的可链接性.

是否有一些方法比将ul添加到列表中更明显但是这与jQuery的处理方式相符?

And*_*ken 5

你应该做的是扩展你的插件的设置:

jQuery.ticker = function(settings)
{
var settings = 
jQuery.extend(
{
action : 'create',
item : $(this)
}
,settings);

return $(this).each(function(){
if(settings.action =='create')
{
  //initialize ticker..
}
else if(settings.action == 'add')
{
  //add to ticker.
}
}//end each.
}//end plugin.

$('#ticker').ticker(); //this will initialize it, no problem.

var settings1 = { action : 'add', item : $(expr1) }
$('#ticker').ticker(settings1);//this will execute the "add" action.
Run Code Online (Sandbox Code Playgroud)