我試圖創建一個jQuery插件,將創造這樣一個autoCompleteBox但自定義功能。如何爲每個匹配的jQuery元素存儲成員變量?如何有成員變量和公共方法的jQuery插件?
比如我需要存儲的timerId每個。我還想存儲對構成控件的一些DOM元素的引用。
我希望能夠做出的作品像一個公共方法:
$("#myCtrl").autoCompleteEx.addItem("1");
不過的addItem()的執行我怎麼能訪問成員變量像它的timerId特定對象管他呢?
下面是我迄今爲止...
感謝任何幫助或建議!
(function($)
{
//Attach this new method to jQuery
$.fn.autoCompleteEx = function(options)
{
//Merge Given Options W/ Defaults, But Don't Alter Either
var opts = $.extend({}, $.fn.autoCompleteEx.defaults, options);
//Iterate over the current set of matched elements
return this.each(function()
{
var acx = $(this); //Get JQuery Version Of Element (Should Be Div)
//Give Div Correct Class & Add <ul> w/ input item to it
acx.addClass("autoCompleteEx");
acx.html("<ul><li class=\"input\"><input type=\"text\"/></li></ul>");
//Grab Input As JQ Object
var input = $("input", acx);
//Wireup Div
acx.click(function()
{
input.focus().val(input.val());
});
//Wireup Input
input.keydown(function(e)
{
var kc = e.keyCode;
if(kc == 13) //Enter
{
}
else if(kc == 27) //Esc
{
}
else
{
//Resize TextArea To Input
var width = 50 + (_txtArea.val().length*10);
_txtArea.css("width", width+"px");
}
});
}); //End Each JQ Element
}; //End autoCompleteEx()
//Private Functions
function junk()
{
};
//Public Functions
$.fn.autoCompleteEx.addItem = function(id,txt)
{
var x = this;
var y = 0;
};
//Default Settings
$.fn.autoCompleteEx.defaults =
{
minChars: 2,
delay: 300,
maxItems: 1
};
//End Of Closure
})(jQuery);
你是一個天才......感謝一百萬! – Legend 2010-06-01 03:06:31