代码
DMPlatform.Tools.tableTools = (function(){
/**内部方法*/
//动态光标行
(function hightlightRow(){
//下面是动态光标行和被选择后的效果。
$(".resultTable tbody tr").live("mouseover",function(){
$(this).addClass("tips_row_hover");
}).live("mouseout",function(){
$(this).removeClass("tips_row_hover");
});
})();
(function checkboxCheck(){
$(".resultTable tbody td").live("click",function(event){//此方法负责行的变色及全选是否选中
var selectAll = $(this).parents("table").find("thead :checkbox:eq(0)");
if(event.target != this){
if($(this).parents("tr").find(":checkbox.selector").attr("checked")){
$(this).parents("tr").addClass("tips_row_selected");// console.info("上色");
if($(this).parents("tbody").find(":checkbox:not([checked]).selector").length == 0){
selectAll.attr("checked","checked");
}
}else{
$(this).parents("tr").removeClass("tips_row_selected");// console.info("褪色");
selectAll.removeAttr("checked");
}
}
});
})();
//表头的全选checkbox
(function checkAll(){
$(".selectAll").live("click",function(){
var rsTable = $(this).parents("table");
if($(this).attr("checked")){
rsTable.find("tbody :checkbox:not(:checked).selector").attr("checked","checked");
rsTable.find("tbody :checkbox").parents("tr").addClass("tips_row_selected");
}else{
rsTable.find("tbody :checkbox:checked.selector").removeAttr("checked");
rsTable.find("tbody tr").removeClass("tips_row_selected");
}
});
})();
//对传入的对象中的所有input类型进行序列化
function serialize(obj){
if(obj==undefined){
throw new Error("DMPlatform.Tools.tableTools:obj of serialize is undefined!");
}
if(typeof(obj)!=="object"){
throw new Error("DMPlatform.Tools.tableTools:obj of serialize typeof isn't Object,is "+typeof(obj));
}
var inputs = $(obj).find(":input,select,textarea");
var inputArray = new Array();
var tempJSON;
inputs.each(function(){
if($(this).attr("name")!==undefined){
tempJSON = new Object();
//如果有服务器端的控件
if($(this).attr("class")=="runat_server"){
tempJSON.name = $(this).attr("name").match(/\w+$/)[0];
}else{
tempJSON.name = $(this).attr("name");
}
tempJSON.value = $(this).val();
inputArray[inputArray.length] = tempJSON;
}
});
return inputArray;
}
return{
getSerializeOnlyJSON:function(obj){
return JSON.stringify(serialize(obj));
},
getSerialize:function(obj){
return serialize(obj);
}
}
})();
以下是简化后抽象出来类似于上面代码的DEMO
代码
<html>
<body>
<script>
TestObj=(function(){
(function A(){alert('A');})();
(function B(){alert('B');})();
return {C:function(){alert('haha')},
D:function(){alert('hehe')}
};
})()
TestObj.C();
TestObj.D();
</script>
</body>
</html>