var _max=this.textareaSettings.max; var _length = $(this).val().length; if(_length > _max) { $(this).val($(this).val().substring(0, _max)); } var _left = $(this).offset().left; var _top = $(this).offset().top; var _width = $(this).width(); var _height = $(this).height(); var divTip=$("<div class='textareaTip'></div>");
var _tip=this.textareaSettings.tip; _tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
var _max=this.textareaSettings.max; var _length = $(this).val().length; if(_length > _max) { $(this).val($(this).val().substring(0, _max)); } var _left = $(this).offset().left; var _top = $(this).offset().top; var _width = $(this).width(); var _height = $(this).height(); var divTip=$("<div class='textareaTip'></div>");
var _tip=this.textareaSettings.tip; _tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
不如去问google
function complex(p1,options){
var settings=$.extends(
{
options1:defaultValue2,
options2:defaultValue2
},options||{});
}2.扩展工具函数
(function($){
$.say=function(what){alert(‘I say ’+what);}
})(jQuery)
3.扩展包装集
(function($){
$.fn.makeItBlue=function(){
return this.css(‘color’,’blue’};
})(jQuery)(function($){
$.fn.makeAllBlue=function(){
return this.each(function(){
this.css(‘color’,’blue’);
})
})(jQuery)4.示例
/**
* textarea插件 计算字数
*/
(function($) {
$.fn.initCountTip = function(settings)
{
return this.each(function()
{
this.textareaSettings = $.extend({
//下拉区域
max:100,
//父元素
tip:"提示:限制输入{max}个字符,已输入{current}个字符!",
}, settings);
var _max=this.textareaSettings.max;
var _length = $(this).val().length;
if(_length > _max) {
$(this).val($(this).val().substring(0, _max));
}
var _left = $(this).offset().left;
var _top = $(this).offset().top;
var _width = $(this).width();
var _height = $(this).height();
var divTip=$("<div class='textareaTip'></div>");
var _tip=this.textareaSettings.tip;
_tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
divTip.html(_tip);
$(this).after(divTip);
$(this).keyup(function() {
$(this).showWordCount();
});
$(this).focus(function(){
$(this).showWordCount();
$(this).find("~div").fadeIn('slow');
});
$(this).blur(function(){
$(this).find("~div").fadeOut('slow');
});
});
},
$.fn.showWordCount=function() {
return this.each(function(){
var _tip=this.textareaSettings.tip;
var _max=this.textareaSettings.max;
var _length = $(this).val().length;
if(_length > _max) {
$(this).val($(this).val().substring(0, _max));
}
_tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
$(this).find("~div").html(_tip);
});
}
})(jQuery);调用方式$("#textareaId").initCountTip({max:100});
<head>
<script src="jquery.js" type="text/javascript"></script>
<script>/**
* textarea插件 计算字数,可以放在单独的jquery.textarea.js文件中
*/
(function($) {$.fn.initCountTip = function(settings)
{
return this.each(function()
{
this.textareaSettings = $.extend({
//最大字符数,默认值为100
max:100,
//提示信息
tip:"提示:限制输入{max}个字符,已输入{current}个字符!"
}, settings);
var _max=this.textareaSettings.max;
var _length = $(this).val().length;
if(_length > _max) {
$(this).val($(this).val().substring(0, _max));
}
var _left = $(this).offset().left;
var _top = $(this).offset().top;
var _width = $(this).width();
var _height = $(this).height();
var divTip=$("<div class='textareaTip'></div>");
var _tip=this.textareaSettings.tip;
_tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
divTip.html(_tip);
$(this).after(divTip);
$(this).keyup(function() {
$(this).showWordCount();
});
$(this).focus(function(){
$(this).showWordCount();
$(this).find("~div").fadeIn('slow');
});
$(this).blur(function(){
$(this).find("~div").fadeOut('slow');
});
});
},
//显示字数
$.fn.showWordCount=function() {
return this.each(function(){
var _tip=this.textareaSettings.tip;
var _max=this.textareaSettings.max;
var _length = $(this).val().length;
if(_length > _max) {
$(this).val($(this).val().substring(0, _max));
}
_tip=_tip.replace("{current}",$(this).val().length).replace("{max}",_max);
$(this).find("~div").html(_tip);
});
}
})(jQuery);
/**
* textarea插件 结束
*/
//初始化
function init(){
$("#test").initCountTip();
//$("#test").initCountTip({max:300,tip:"提示:最大{max},已输入{current}!"});
}
$(document).ready(init);
</script></head>
<body>
<form>
<textarea id="test" cols="40" rows="10"></textarea>
</form>
</body>
</html>
//用法: $().yourPlugin
$.fn.yourPlugin=function(){};
//用法: $.yourPlugin
$.yourPlugin=function(){};
})();