jquery插件开发-简略实践
jQuery为开发插件提拱了两个方法,分别是:
jQuery.fn.extend(object); 给jQuery对象添加方法。
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
下面是一个简单的表单列表的全选/反选以及列表的奇偶行背景简单的编写的小插件
(function ($) {
//checkbox 批量处理插件
$.fn.selectCheckBox = function () {
var selectboxs = this;
return selectboxs.each(function (index) {
$(this).click(function () {
if (index == 0 ) {
if ($(this).is(':checked')) {
selectboxs.prop("checked",'checked');
} else {
selectboxs.removeAttr("checked");
}
} else {
if($(this).is(':checked')){
var checked_length = $("input[type='checkbox']:checked").length;
if(selectboxs.first().prop('checked') == false && (checked_length == selectboxs.length-1 )){
selectboxs.first().prop("checked",'checked');
}
}else{
selectboxs.first().removeAttr("checked");
}
}
});
});
};
该如何使用呢,一般引入上述js:
调用如下,#test:id为test的table
$(function(){
$('#test').tableBgColor();
$("input[type='checkbox']").selectCheckBox();
};
是不是很简单,试下呗