//去处字符串两端空格
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g,"");
}
//寻找value在数组中的位置
Array.prototype.indexOf = function(value)
{
var idx = -1;
for(var i=0;i<this.length;i++)
{
if(this[i] == value)
{
idx = i;
break;
}
}
return idx;
}
//判断对象是否为空
function isNull(param)
{
return (typeof(param) == "undefined" || param == null)
}
//表单常用方法
function HTMLUtility(){}
function HTMLUtility.openDialog(sURL,target,param,w,h,resizable)
{
var args = isNull(param)? window : param;
w = isNull(w)? 400 : w;
h = isNull(h)? 300 : h;
resizable = isNull(resizable)? "no":"yes";
//resizable = "yes";
var rand = Math.random() * 34343;
var strRand = "tmp=" + rand;
sURL = (sURL.indexOf("?") > -1)? (sURL+"&" + strRand) : (sURL+ "?" + strRand);
var sFeature = "dialogWidth:"+ w +"px;dialogHeight:"+ h +"px;center:yes;status:no;help:no;scroll:yes;resizable="+ resizable+"";
var retValue = window.showModalDialog(sURL,args,sFeature);
//目标赋值对象为空,则返回dialog的returnValue
if(!isNull(target))
{
if(!isNull(retValue))
{
if(target.type == "text")
target.value = retValue;
else if(target.tagName = "div")
target.innerText = retValue;
}
}
else
{
return retValue;
}
}
//删除确认
function HTMLUtility.delConfirm(info)
{
var src = event.srcElement;
var isDel = false;
//提交按钮
if(src.type == "submit" && src.value == "删除")
{
isDel = true
}//连接按钮
else if(src.tagName.toLowerCase() == "a" && src.innerText == "删除")
{
isDel = true
}
else
{
isDel = false
}
info = isNull(info)? "确定删除吗?" : info;
if(isDel)
{
window.event.returnValue = confirm(info);
}
}
//获取地址栏参数,param为参数名
function HTMLUtility.getURLValue(param)
{
var local = location.search;
//var local = "?pos=abc&dudu=123";
param += "=";
local = (local.indexOf("?") == 0)? local.substr(1,local.length): local;
var reg = param + "[^&]*";
var value = (local.match(reg) == null)? "" : local.match(reg).toString().replace(param,"");
//alert(value);
return value;
}
//获取对象的绝对位置信息
function HTMLUtility.getAbsolutePosition(element)
{
if ( arguments.length != 1 || element == null )
{
return null;
}
var offsetTop = element.offsetTop;
var offsetLeft = element.offsetLeft;
var offsetWidth = element.offsetWidth;
var offsetHeight = element.offsetHeight;
while(element = element.offsetParent)
{
offsetTop += element.offsetTop;
offsetLeft += element.offsetLeft;
}
return { absoluteTop: offsetTop, absoluteLeft: offsetLeft,
offsetWidth: offsetWidth, offsetHeight: offsetHeight };
}
function HTMLUtility.clearMessage(obj)
{
//window.setTimeout("doFun",3000);
}
//复位表单元素
function HTMLUtility.resetForm()
{
var form = document.forms[0];
var e;
for(var i=0;i<form.elements.length;i++)
{
e = form.elements[i];
if(e.type == "text") e.value = "";
else if(e.type == "check") e.checked = false;
//else if(e.type == "hidden") e.value = "";
else if(e.type == "textarea") e.value = "";
}
}
function HTMLUtility.checkAllorNot()
{
var self = event.srcElement;
var form = document.forms[0];
var ele;
for(var i=0;i<form.elements.length;i++)
{
ele = form.elements[i];
if(ele.type.toLowerCase() == "checkbox")
{
ele.checked = self.checked;
ele.attachEvent("onclick",uncheckParent);
}
}
function uncheckParent()
{
var child = event.srcElement;
if(!child.checked) self.checked = false;
}
}
// TreeView相关操作
/************************************
var mgr = TreeViewManager();
mgr.TreeView = document.getElementByID("tvDepartment");
var ids = mgr.GetCheckedNodes();
************************************/
function TreeViewManager()
{
this.TreeView = null;
//勾选当前点选的所有下级节点
this.CheckedSubNodes = function()
{
var tv = this.TreeView;
//当前点击的节点
var node = tv.getTreeNode(tv.clickedNodeIndex);
_checkedNodes(node);
}
//取得所有选中节点的ID值,数组返回
this.GetCheckedNodes = function()
{
var tv = this.TreeView;
var a = getTvNodes(tv.getChildren());
return a.join(",");
function getTvNodes(nodes)
{
var matches = [];
var node,isCheck;
for(var i=0;i<nodes.length;i++)
{
node = nodes[i];
isCheck = node.getAttribute("checked");
if(isCheck)
{
matches.push(node.getAttribute("id"));
}
if(hasChild(node))
{
//递归
matches = matches.concat(getTvNodes(node.getChildren()));
}
}
return matches
}
}
//该节点是否有下级节点
this.HasChild = function(node)
{
return node.getChildren().lenght>0;
}
function _checkedNodes(node)
{
var isCheck = node.getAttribute("checked");
setNodes(node,isCheck);
function setNodes(node,isCheck)
{
var nodes = node.getChildren();
var node;
for(var i=0;i<nodes.length;i++)
{
node = nodes[i];
node.setAttribute("checked",isCheck);
setNodes(node,isCheck);
}
}
}
}
//DHTML中节点搜寻对象
/**********************************************
var nodeMgr = NodeSearchManager();
nodeMgr.SearchNode = document.getElementById("myTable");
//可以重写node的搜索条件,如:
NodeSearchManager.prototype.SearchCondition = function(node)
{
//some condition
}
var myNodes = nodeMgr.FindNodes();
**********************************************/
function NodeSearchManager()
{
//要搜寻的节点对象
this.SearchNode = null;
//返回满足条件的节点集
this.FindNodes = function()
{
if(this.SearchNode != null)
return this._findNoes(this.SearchNode);
else
return null;
}
this._findNoes = function(node)
{
var matches = [];
for(var i=0;i<node.childNodes.length;i++)
{
var currentNode = node.childNodes[i];
var isValid = this.SearchCondition(currentNode);
if(isValid)
{
matches[matches.length] = currentNode;
}
matches = matches.concat(this._findNoes(currentNode));
}
return matches;
}
}
//默认的搜寻函数,实际使用中可以重写
NodeSearchManager.prototype.SearchCondition = function(node)
{
return (node.tagName != undefined && node.tagName.toLowerCase() == "div");
}
//cookie管理对象
function CookieManager()
{
this._Cookie=[];
this.Load=function(){
if(document.cookie.indexOf(";")!=-1){
var _sp,_name,_tp,_tars,_tarslength;
var _item=document.cookie.split("; ");
var _itemlength=_item.length;
while(_itemlength>0){
_sp=_item[--_itemlength].split("=");
_name=_sp[0];
_tp=_sp[1].split(",");
_tars=_tp.slice(1,_tp.length);
this._Cookie[_name]=[];
this._Cookie[_name]=_tars;
this._Cookie[_name]["timeout"]=_tp[0];
}
return true;
}
return false;
}
this.Save=function(){
var _str,_ars,_mars,_marslength,timeout,i,key;
for(key in this._Cookie){
if(!this._Cookie[key])return;
_str=[];
_mars=this._Cookie[key];
_marslength=_mars.length;
for(i=0;i<_marslength;i++)_str[_str.length]=escape(_mars[i]);
document.cookie=key+"="+_mars["timeout"]+(_str.length>0?",":"")+_str+(_mars["timeout"]==0?"":";expires="+new Date(parseInt(_mars["timeout"])).toGMTString());
}
}
this.GetCookieCount=function(){
var _length=0,key;
for(key in this._Cookie)_length++;
return _length;
}
this.Create=function(name,days){
days=days?days:0;
if(!this._Cookie[name])this._Cookie[name]=[];
this._Cookie[name]["timeout"]=(days!=0)?new Date().getTime()+parseInt(days)*86400000:0;
}
this.Modify=function(name,days){
this.Create(name,days);
}
this.GetTime=function(name){
return new Date(parseInt(this._Cookie[name]["timeout"]));
}
this.Delete=function(name){
this.Create(name,0);
}
this.AddItem=function(name,value){
this._Cookie[name][this._Cookie[name].length]=value;
}
this.DelItem=function(name,index){
var _ttime=this._Cookie[name]["timeout"];
this._Cookie[name]=this._Cookie[name].slice(0,index).concat(this._Cookie[name].slice(parseInt(index)+1,this._Cookie[name].length));
this._Cookie[name]["timeout"]=_ttime;
}
this.GetCount=function(name){
return this._Cookie[name].length;
}
this.GetItem=function(name,index){
return this._Cookie[name][index];
}
this.GetItems=function(name){
return this._Cookie[name];
}
}