`
JavaSam
  • 浏览: 952002 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

项目中写的一些比较常用的javascript通用函数

 
阅读更多
美化下拉框
var childCreate = false;
function Offset(e) {
	//取标签的绝对位置
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight - 2;
	while ( e = e.offsetParent) {
		t += e.offsetTop;
		l += e.offsetLeft;
	}
	return {
		top : t,
		left : l,
		width : w,
		height : h
	}
}

function loadselect(obj) {
	//第一步:取得select所在的位置
	var offset = Offset(obj);
	//第二步:将真的select隐藏
	obj.style.display = "none";
	//第三步:虚拟一个div出来代替select
	var iDiv = document.createElement("div");
	iDiv.id = "selectof" + obj.name;
	iDiv.style.position = "absolute";
	iDiv.style.width = offset.width + "px";
	iDiv.style.height = offset.height + "px";
	iDiv.style.top = offset.top + 4 + "px";
	iDiv.style.left = offset.left + 4 + "px";
	iDiv.style.background = "url('test.png') no-repeat right -6px";
	iDiv.style.border = "1px solid #ccc";
	iDiv.style.fontSize = "12px";
	iDiv.style.lineHeight = offset.height + "px";
	iDiv.style.textIndent = "4px";
	document.body.appendChild(iDiv);
	//第四步:将select中默认的选项显示出来
	var tValue = obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML = tValue;
	//第五步:模拟鼠标点击
	iDiv.style.background = "url('images/select.jpg') no-repeat right -2px";
	iDiv.onclick = function() {//鼠标点击
		if (document.getElementById("selectchild" + obj.name)) {
			//判断是否创建过div
			if (childCreate) {
				//判断当前的下拉是不是打开状态,如果是打开的就关闭掉。是关闭的就打开。
				document.getElementById("selectchild" + obj.name).style.display = "none";
				childCreate = false;
			} else {
				document.getElementById("selectchild" + obj.name).style.display = "";
				childCreate = true;
			}
		} else {
			//初始一个div放在上一个div下边,当options的替身。
			var cDiv = document.createElement("div");
			cDiv.id = "selectchild" + obj.name;
			cDiv.style.position = "absolute";
			cDiv.style.width = offset.width + "px";
			cDiv.style.height = obj.options.length * 20 + "px";
			cDiv.style.top = (offset.top + offset.height + 2) + 4+"px";
			cDiv.style.left = offset.left + 4+ "px";
			cDiv.style.background = "#f7f7f7";
			cDiv.style.border = "1px solid silver";
			var uUl = document.createElement("ul");
			uUl.id = "uUlchild" + obj.name;
			uUl.style.listStyle = "none";
			uUl.style.margin = "0";
			uUl.style.padding = "0";
			uUl.style.fontSize = "12px";
			cDiv.appendChild(uUl);
			document.body.appendChild(cDiv);
			childCreate = true;
			for (var i = 0; i < obj.options.length; i++) {
				//将原始的select标签中的options添加到li中
				var lLi = document.createElement("li");
				lLi.id = obj.options[i].value;
				lLi.style.textIndent = "4px";
				lLi.style.height = "20px";
				lLi.style.lineHeight = "20px";
				lLi.innerHTML = obj.options[i].innerHTML;
				uUl.appendChild(lLi);
			}
			var liObj = document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
			for (var j = 0; j < obj.options.length; j++) {
				//为li标签添加鼠标事件
				liObj[j].onmouseover = function() {
					this.style.background = "gray";
					this.style.color = "white";
				}
				liObj[j].onmouseout = function() {
					this.style.background = "white";
					this.style.color = "black";
				}
				liObj[j].onclick = function() {
					//做两件事情,一是将用户选择的保存到原始select标签中,要不做的再好看表单递交后也获取不到select的值了。
					obj.options.length = 0;
					obj.options[0] = new Option(this.innerHTML, this.id);
					//同时我们把下拉的关闭掉。
					document.getElementById("selectchild" + obj.name).style.display = "none";
					childCreate = false;
					iDiv.innerHTML = this.innerHTML;
				}
			}
		}
	}
}
document.body.onload = function (){
	var selects = document.getElementsByTagName("select");
	for(var i = 0 ; i < selects.length;i++){
		loadselect(selects[i]);
	}
}

仿java类实现(持续更新中)
/**
 * Map实现
 */
var Map = function() {
	this.flag = false;
	this.store = new Array();
	if (!this.flag) {
		Map.prototype.entry = function(key, val) {
			this.key = key;
			this.val = val;
		}
		Map.prototype.put = function(key, val) {
			this.store[this.store.length] = new this.entry(key, val);
		}
		Map.prototype.get = function(key) {
			for (var i = this.store.length - 1; i >= 0; i--) {
				if (this.store[i].key === key)
					return this.store[i].val;
			}
			return null;
		}
		Map.prototype.remove = function(key) {
			for (var i = this.store.length - 1; i >= 0; i--) {
				this.store[i].key === key && this.store.splice(i, 1);
			}
		}
		Map.prototype.keySet = function() {
			var keys = new Array();
			for (var i = 0; i <= this.store.length - 1; i++) {
				keys.push(this.store[i].key);
			}
			return keys;
		}
		Map.prototype.valSet = function() {
			var vals = new Array();
			for (var i = 0; i <= this.store.length - 1; i++) {
				vals.push(this.store[i].val);
			}
			return vals;
		}
		Map.prototype.clear = function() {
			this.store.length = 0;
		}
		Map.prototype.size = function() {
			return this.store.length;
		}
		this.flag = true;
	}
}
/**
 * StringBuilder实现
 */
var StringBuilder = function() {
	this.vStrings = new Array();
	this.flag = false;
	if (!this.flag) {
		StringBuilder.prototype.append = function(str) {
			this.vStrings.push(str);
		}
		StringBuilder.prototype.toString = function() {
			return this.vStrings.join("");
		}
		StringBuilder.prototype.charAt = function(index) {
			return this.toString().charAt(index);
		}
		StringBuilder.prototype.clear = function() {
			this.vStrings.length = 0;
		}
		StringBuilder.prototype.Delete = function(start, end) {
			var tempString = this.toString();
			var prevString = tempString.substring(0, start);
			var nextString = end ? tempString.substring(end) : tempString.substring(start + 1);
			this.clear();
			this.append(prevString);
			this.append(nextString);
		}
		StringBuilder.prototype.length = function() {
			return this.toString().length;
		}
		StringBuilder.prototype.substring = function(start, end) {
			return this.toString().substring(start, end);
		}
		StringBuilder.prototype.replace = function(oldStr, newStr) {
			var newStr = newStr ? newStr : "";
			var tempString = this.toString().replace(new RegExp(oldStr, "g"), newStr);
			this.clear();
			this.append(tempString);
		}
		StringBuilder.prototype.indexOf = function(val) {
			return this.toString().indexOf(val);
		}
		StringBuilder.prototype.lastIndexOf = function(val) {
			return this.toString().lastIndexOf(val);
		}
		StringBuilder.prototype.insert = function(offset, str) {
			var prevString = this.substring(0, offset);
			var middleString = str;
			var nextString = this.substring(offset);
			this.clear();
			this.append(prevString);
			this.append(middleString);
			this.append(nextString);
		}
		StringBuilder.prototype.split = function(sep) {
			return this.toString().split(sep);
		}
		this.flag = true;
	}
}


为类库对象加入新方法
/**
 * @author wsf
 */
 /**
  * 判断是否为数字型数据
  */
 if (!Object.prototype.isNumber) {
	 String.prototype.isNumber = function() {
		 return !this || isNuN(this.val()) ? false : (typeof this.val() === "number");
	 }
 }
 /**
  * 为数组添加remove方法
  */
 if (!Array.prototype.remove) {
	 Array.prototype.remove = function(s) {
		 for (var i = this.length; i >= 0; i--) {
			 if (s === this[i])
				 this.splice(i, 1);
		 }
	 }
 }
/**
 * 为数组添加push方法
 */
if (!Array.prototype.push) {
	 Array.prototype.push = function() {
		 var B = this.length;
		 for (var A = 0; A < arguments.length; A++) {
			 this[B + A] = arguments[A]
		 }
		 return this.length
	 }
}
 /**
  * 为数组添加indexOf方法
  */
 if (!Array.prototype.indexOf) {
	 Array.prototype.indexOf = function(o) {
		 for (var i = this.length; i >= 0; i--) {
			 if (this[i] === o)
				 return i;
		 }
		 return -1;
	 }
 }
 /**
  * 为数组添加contain方法
  */
 if (!Array.prototype.contain) {
	 Array.prototype.contain = function(o) {
		 var validx = this.indexOf(o)//拿到索引
		 return validx > 0;
	 }
 }
 /**
  * 为数组添加put方法(添加的到指定下标位置)
  */
 if (!Array.prototype.put) {
	 Array.prototype.put = function(o, idx) {
		 return this.splice(idx, 0, o);
	 }
 }
/**
 * 为字符对象添加intVal方法
 */
if (!String.prototype.intVal) {
	String.prototype.intVal = function() {
		return parseInt(this);
	}
}
/**
 * 为字符对象添加doubleVal方法
 */
if (!String.prototype.floatVal) {
	String.prototype.floatVal = function() {
		return parseFloat(this);
	}
}
/**
 * 为String添加trim方法
 */
if(!String.prototype.trim){
	String.prototype.trim = function (){
		this.replace(/\s*|\s$/g,"");
	}
}

分页

/**
* 创建实例的的方法
* @param {Object} Class
* @param {Object} params
*/
var newObj = function (Class,params){
	function _new (){
		if(Class._new){
			Class._new.call(this,params);//改变this指向
		}
	}
	_new.prototype = Class;
	return new _new();
}

/**
* page对象
*/
var page = {
	/**
	 * 构造函数
	 * @param {Object} pageSize :一页显示多少条
	 * @param {Object} dataTotalCount :总共数据条数
	 * @param {Object} mode : 分页模式 (1:不显示页数,只显示上一页下一页;2:待续);
	 * @param {Object} pageItem :一页显示多少条目页
	 * @param {Object} pageContainer :容器
	 * @param {Function} selfReqFun : 自定义请求函数
	 */
	_new : function(options){
		var pageItem = options.pageItem;
		var pageContainer = options.pageContainer;
		var selfReqFun = options.selfReqFun;
		/**
		 * 初始化page属性
		 */
		this.pageNo = options.pageNo;
		this.pageSize = options.pageSize;
		this.dataTotalCount = options.dataTotalCount;
		this.mode = options.mode;
		this.pageItem = pageItem == ""||pageItem == null ? this.mode = 1 : pageItem;
		this.pageContainer = pageContainer == "" || pageContainer == null ? (function (){
			var container = document.createElement("div");
				container.id = "pages";
				container.style.textAlign = "center";
				container.align = "center";
			document.getElementsByTagName("body")[0].appendChild(container);
			return container;
		})() : pageContainer;
		this.pageCount = Math.ceil(this.dataTotalCount/this.pageSize);
		this.selfReqFun = selfReqFun == null ? function(){} : selfReqFun;
		this.itemStyle = options.itemStyle;
		this.selfArgs = options.selfArgs;
	},
	/**
	 * 注册事件
	 * @param {Object} target
	 * @param {Object} eventName
	 * @param {Object} handler
	 * @param {Object} args
	 */
	attachEvent : function  (target,eventName,handler,args){
		var eventHandler = handler;
		if(args){
			eventHandler = function(e){
				handler.call(args,e);
			}
		}
	    if(window.attachEvent){
	        target.attachEvent("on" + eventName, eventHandler );   
		} else{
	        target.addEventListener(eventName, eventHandler, true); 
		}   
	},
	/**
	 * 检查pageNo是否合法
	 * @param {Object} pageNo
	 */
	checkPageNo : function(pageNo){
		 if(pageNo == ""  || pageNo == null || pageNo < 1) pageNo = 1;
		 if(pageNo > this.pageCount)pageNo = this.pageCount;
		 return pageNo;
	},
	/**
	 * 创建分页条目
	 * @param {Object} pageNo
	 */
	createPageTab : function(){
		var flag = false;
		//动态添加属性
		this.pageNo = this.checkPageNo(arguments[0]||1);
		var html = '';
		if(this.mode == 1){
				html += '<span title="首页" class="firstPage pageSpan reqSpan" id="first"><<</span>';
				html += '<span title="上一页" class="prevPage pageSpan reqSpan" id="prev">&nbsp;&nbsp;<&nbsp;</span>';
				html += '<span class="pageNo" id = '+this.pageNo+'>'+this.pageNo+'/'+this.pageCount+'</span>';
				html += '<span title="下一页" class="nextPage pageSpan reqSpan" id="next">&nbsp;>&nbsp;&nbsp;</span>';
				html += '<span title="末页" class="lastPage pageSpan reqSpan" id="last">>></span>';
		}
		this.pageContainer.innerHTML = html;
		if(this.itemStyle){
			var itemStyle = this.itemStyle;
			for(var i in itemStyle){
				//判断对象是否为空
				flag = true;
			}
			if (flag) {
				var first = document.getElementById("first");
				var prev  = document.getElementById("prev");
				var next = document.getElementById("next");
				var last = document.getElementById("last");
				first.innerHTML = "<img src="+ itemStyle.firstImg +" style='width:14px;height:14px;cursor:pointer;vertical-align:middle;'/>";
				prev.innerHTML = "<img src="+ itemStyle.prevImg +" style='width:14px;height:14px;cursor:pointer;vertical-align:middle;'/>"
				next.innerHTML = "<img src="+ itemStyle.nextImg +" style='width:14px;height:14px;cursor:pointer;vertical-align:middle;'/>"
				last.innerHTML = "<img src="+ itemStyle.lastImg +" style='width:14px;height:14px;cursor:pointer;vertical-align:middle;'/>"
			}
				
		}
		/**
		 * 注册点击事件
		 */
	   var reqSpans = this.pageContainer.getElementsByTagName("span");
	   for(var j = 0 ; j < reqSpans.length ; j++){
	   		var _this = reqSpans[j];
			_this.style.cursor="pointer";
	   		if((_this.className || _this.getAttribute("class")).indexOf("reqSpan") > 0){
				this.attachEvent(_this,"click",this.requestEvent,this);
			}
	   }
	   //this.selfReqFun(this.pageNo);
	},
	/**
	 * 请求函数
	 * @param {Object} pageNo
	 * @param {Object} id
	 */
	requestEvent : function(e){
		var target = e.srcElement || e.target;
		if(target.nodeName.toLowerCase() === "img"){
			target = target.parentNode;
		}
		var id = target.id;
		var pageNo = this.pageNo
		pageNo = (id === "first")? 1 :
		(id === "prev" && pageNo >= 1) ? this.checkPageNo(pageNo-1):
		(id === "next" && pageNo <= this.pageCount) ? this.checkPageNo(pageNo+1):
		(id === "last")? this.pageCount : 1;
		this.selfReqFun(pageNo);
	}				
}		



自定义弹出框
/**
 * 选择器
 * @return {}
 */
function G() {
	var C = new Array();
	for (var B = 0; B < arguments.length; B++) {
		var A = arguments[B];
		if ( typeof A == "string") {
			A = document.getElementById(A)
		}
		if (arguments.length == 1) {
			return A
		}
		C.push(A)
	}
	return C
}
/**
 * 为函数对象添加bind方法
 * @param {} B
 * @return {}
 */
Function.prototype.bind = function(B) {
	var A = this;
	return function() {
		A.apply(B, arguments)
	}
};
/**
 * @param {} B
 * @return {}
 */
Function.prototype.bindAsEventListener = function(B) {
	var A = this;
	return function(C) {
		A.call(B, C || window.event)
	}
};
/**
 * javascript继承机制
 * @param {} A
 * @param {} B
 * @return {}
 */
Object.extend = function(A, B) {
	for (property in B) {
		A[property] = B[property]
	}
	return A
};
/**
 *自定义event对象 
 */
if (!window.Event) {
	var Event = new Object()
}
Object.extend(Event, {
	observers : false,
	element : function(A) {
		return A.target || A.srcElement
	},
	isLeftClick : function(A) {
		return (((A.which) && (A.which == 1)) || ((A.button) && (A.button == 1)))
	},
	pointerX : function(A) {
		return A.pageX || (A.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft))
	},
	pointerY : function(A) {
		return A.pageY || (A.clientY + (document.documentElement.scrollTop || document.body.scrollTop))
	},
	stop : function(A) {
		if (A.preventDefault) {
			A.preventDefault();
			A.stopPropagation()
		} else {
			A.returnValue = false;
			A.cancelBubble = true
		}
	},
	findElement : function(C, B) {
		var A = Event.element(C);
		while (A.parentNode && (!A.tagName || (A.tagName.toUpperCase() != B.toUpperCase()))) {
			A = A.parentNode
		}
		return A
	},
	_observeAndCache : function(D, C, B, A) {
		if (!this.observers) {
			this.observers = []
		}
		if (D.addEventListener) {
			this.observers.push([D, C, B, A]);
			D.addEventListener(C, B, A)
		} else {
			if (D.attachEvent) {
				this.observers.push([D, C, B, A]);
				D.attachEvent("on" + C, B)
			}
		}
	},
	unloadCache : function() {
		if (!Event.observers) {
			return
		}
		for (var A = 0; A < Event.observers.length; A++) {
			Event.stopObserving.apply(this, Event.observers[A]);
			Event.observers[A][0] = null
		}
		Event.observers = false
	},
	observe : function(D, C, B, A) {
		var D = G(D);
		A = A || false;
		if (C == "keypress" && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || D.attachEvent)) {
			C = "keydown"
		}
		this._observeAndCache(D, C, B, A)
	},
	stopObserving : function(D, C, B, A) {
		var D = G(D);
		A = A || false;
		if (C == "keypress" && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || D.detachEvent)) {
			C = "keydown"
		}
		if (D.removeEventListener) {
			D.removeEventListener(C, B, A)
		} else {
			if (D.detachEvent) {
				D.detachEvent("on" + C, B)
			}
		}
	}
});
Event.observe(window, "unload", Event.unloadCache, false);
var Class = function() {
	var A = function() {
		this.initialize.apply(this, arguments)
	};
	for ( i = 0; i < arguments.length; i++) {
		superClass = arguments[i];
		for (member in superClass.prototype) {
			A.prototype[member] = superClass.prototype[member]
		}
	}
	A.child = function() {
		return new Class(this)
	};
	A.extend = function(B) {
		for (property in B) {
			A.prototype[property] = B[property]
		}
	};
	return A
};
var Popup = new Class();
Popup.prototype = {
	iframeIdName : "ifr_popup",
	initialize : function(A) {
		this.config = Object.extend({
			contentType : 1,
			isHaveTitle : true,
			scrollType : "no",
			isBackgroundCanClick : false,
			isSupportDraging : true,
			isShowShadow : true,
			isReloadOnClose : true,
			width : 400,
			isPop : false,
			height : 300
		}, A || {});
		this.info = {
			shadowWidth : 4,
			title : "",
			contentUrl : "",
			contentHtml : "",
			callBack : null,
			parameter : null,
			confirmCon : "",
			alertCon : "",
			someHiddenTag : "select,object,embed",
			someDisabledBtn : "",
			someHiddenEle : "",
			overlay : 0,
			coverOpacity : 40
		};
		this.color = {
			cColor : "#EEEEEE",
			bColor : "#FFFFFF",
			tColor : "#709CD2",
			wColor : "#FFFFFF"
		};
		this.dropClass = null;
		this.someToHidden = [];
		this.someToDisabled = [];
		if (!this.config.isHaveTitle) {
			this.config.isSupportDraging = false
		}
		this.iniBuild()
	},
	setContent : function(A, B) {
		if (B != "") {
			switch(A) {
				case"width":
					this.config.width = B;
					break;
				case"height":
					this.config.height = B;
					break;
				case"title":
					this.info.title = B;
					break;
				case"contentUrl":
					this.info.contentUrl = B;
					break;
				case"contentHtml":
					this.info.contentHtml = B;
					break;
				case"callBack":
					this.info.callBack = B;
					break;
				case"parameter":
					this.info.parameter = B;
					break;
				case"confirmCon":
					this.info.confirmCon = B;
					break;
				case"alertCon":
					this.info.alertCon = B;
					break;
				case"someHiddenTag":
					this.info.someHiddenTag = B;
					break;
				case"someHiddenEle":
					this.info.someHiddenEle = B;
					break;
				case"someDisabledBtn":
					this.info.someDisabledBtn = B;
					break;
				case"overlay":
					this.info.overlay = B
			}
		}
	},
	iniBuild : function() {
		G("dialogCase") ? G("dialogCase").parentNode.removeChild(G("dialogCase")) : function() {
		};
		var A = document.createElement("span");
		A.id = "dialogCase";
		document.body.insertBefore(A, document.body.firstChild)
	},
	build : function() {
		var A = 10001 + this.info.overlay * 10;
		var B = A + 2;
		this.iframeIdName = "ifr_popup" + this.info.overlay;
		var D = "images/close.gif";
		var F = '<input type="image" id="dialogBoxClose" src="' + D + '" border="0" width="10" height="16" align="absmiddle" title="关闭"/>';
		var H = "filter: alpha(opacity=" + this.info.coverOpacity + ");opacity:" + this.info.coverOpacity / 100 + ";";
		var C = '<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:' + document.body.scrollHeight + "px;z-index:" + A + ";" + H + "background-color:" + this.color.cColor + ';display:none;"></div>';
		var E = '<div id="dialogBox" style="border:1px solid ' + this.color.tColor + ";display:none;z-index:" + B + ";position:relative;width:" + this.config.width + 'px;"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="' + this.color.bColor + '">';
		if (this.config.isHaveTitle) {
			E += '<tr height="24" bgcolor="' + this.color.tColor + '"><td><table style="-moz-user-select:none;height:24px;" width="100%" border="0" cellpadding="0" cellspacing="0" ><tr><td width="6" height="24"></td><td id="dialogBoxTitle" style="color:' + this.color.wColor + ';font-size:14px;font-weight:bold;">' + this.info.title + '&nbsp;</td><td id="dialogClose" width="20" align="right" valign="middle">' + F + '</td><td width="6"></td></tr></table></td></tr>'
		} else {
			E += '<tr height="10"><td align="right">' + F + "</td></tr>"
		}
		E += '<tr style="height:' + this.config.height + 'px" valign="top"><td id="dialogBody" style="position:relative;"></td></tr></table></div><div id="dialogBoxShadow" style="display:none;z-index:' + A + ';"></div>';
		if (!this.config.isBackgroundCanClick) {
			G("dialogCase").innerHTML = C + E;
			G("dialogBoxBG").style.height = document.body.scrollHeight
		} else {
			G("dialogCase").innerHTML = E
		}
		Event.observe(G("dialogBoxClose"), "click", this.reset.bindAsEventListener(this), false);
		if (this.config.isSupportDraging) {
			dropClass = new Dragdrop(this.config.width, this.config.height, this.info.shadowWidth, this.config.isSupportDraging, this.config.contentType);
			G("dialogBoxTitle").style.cursor = "move"
		}
		this.lastBuild()
	},
	lastBuild : function() {
		var B = '<div style="width:100%;height:100%;text-align:center;"><div style="margin:20px 20px 0 20px;font-size:14px;line-height:16px;color:#000000;">' + this.info.confirmCon + '</div><div style="margin:20px;"><input id="dialogOk" type="button" value="  确定  "/>&nbsp;<input id="dialogCancel" type="button" value="  取消  "/></div></div>';
		var E = '<div style="width:100%;height:100%;text-align:center;"><div style="margin:20px 20px 0 20px;font-size:14px;line-height:16px;color:#000000;">' + this.info.alertCon + '</div><div style="margin:20px;"><input id="dialogYES" type="button" value="  确定  "/></div></div>';
		var A = 10001 + this.info.overlay * 10;
		var D = A + 4;
		if (this.config.contentType == 1) {
			var C = "<iframe width='100%' style='height:" + this.config.height + "px' name='" + this.iframeIdName + "' id='" + this.iframeIdName + "' src='" + this.info.contentUrl + "' frameborder='0' scrolling='" + this.config.scrollType + "'></iframe>";
			var F = "<div id='iframeBG' style='position:absolute;top:0px;left:0px;width:1px;height:1px;z-index:" + D + ";filter: alpha(opacity=00);opacity:0.00;background-color:#ffffff;'><div>";
			G("dialogBody").innerHTML = C + F
		} else {
			if (this.config.contentType == 2) {
				G("dialogBody").innerHTML = this.info.contentHtml
			} else {
				if (this.config.contentType == 3) {
					G("dialogBody").innerHTML = B;
					Event.observe(G("dialogOk"), "click", this.forCallback.bindAsEventListener(this), false);
					Event.observe(G("dialogCancel"), "click", this.close.bindAsEventListener(this), false)
				} else {
					if (this.config.contentType == 4) {
						G("dialogBody").innerHTML = E;
						Event.observe(G("dialogYES"), "click", this.close.bindAsEventListener(this), false)
					}
				}
			}
		}
	},
	reBuild : function() {
		G("dialogBody").height = G("dialogBody").clientHeight;
		this.lastBuild()
	},
	show : function() {
		this.hiddenSome();
		this.middle();
		if (this.config.isShowShadow) {
			this.shadow()
		}
	},
	forCallback : function() {
		return this.info.callBack(this.info.parameter)
	},
	shadow : function() {
		var A = G("dialogBoxShadow");
		var B = G("dialogBox");
		A.style["position"] = "absolute";
		A.style["background"] = "#000";
		A.style["display"] = "";
		A.style["opacity"] = "0.2";
		A.style["filter"] = "alpha(opacity=20)";
		A.style["top"] = B.offsetTop + this.info.shadowWidth;
		A.style["left"] = B.offsetLeft + this.info.shadowWidth;
		A.style["width"] = B.offsetWidth;
		A.style["height"] = B.offsetHeight
	},
	middle : function() {
		if (!this.config.isBackgroundCanClick) {
			G("dialogBoxBG").style.display = ""
		}
		var F = G("dialogBox");
		F.style["position"] = "absolute";
		F.style["display"] = "";
		var C = (document.documentElement.clientWidth || document.body.clientWidth);
		var E = (document.documentElement.clientHeight || document.body.clientHeight);
		var B = (document.documentElement.scrollTop || document.body.scrollTop);
		var D = (C / 2) - (F.offsetWidth / 2);
		var H = -80 + (E / 2 + B) - (F.offsetHeight / 2);
		var A = H > 0 ? H : (E / 2 + B) - (F.offsetHeight / 2);
		if (A < 1) {
			A = "20"
		}
		if (D < 1) {
			D = "20"
		}
		F.style["left"] = D + "px";
		F.style["top"] = A + "px"
	},
	reset : function() {
		this.close();
		if (this.config.isReloadOnClose) {
			top.location.reload()
		}
	},
	close : function() {
		G("dialogBox").style.display = "none";
		if (!this.config.isBackgroundCanClick) {
			G("dialogBoxBG").style.display = "none"
		}
		if (this.config.isShowShadow) {
			G("dialogBoxShadow").style.display = "none"
		}
		G("dialogBody").innerHTML = "";
		this.showSome()
	},
	hiddenSome : function() {
		var A = this.info.someHiddenTag.split(",");
		if (A.length == 1 && A[0] == "") {
			A.length = 0
		}
		for (var B = 0; B < A.length; B++) {
			this.hiddenTag(A[B])
		}
		var C = this.info.someHiddenEle.split(",");
		if (C.length == 1 && C[0] == "") {
			C.length = 0
		}
		for (var B = 0; B < C.length; B++) {
			this.hiddenEle(C[B])
		}
		var C = this.info.someDisabledBtn.split(",");
		if (C.length == 1 && C[0] == "") {
			C.length = 0
		}
		for (var B = 0; B < C.length; B++) {
			this.disabledBtn(C[B])
		}
	},
	disabledBtn : function(B) {
		var A = document.getElementById(B);
		if ( typeof (A) != "undefined" && A != null && A.disabled == false) {
			A.disabled = true;
			this.someToDisabled.push(A)
		}
	},
	hiddenTag : function(B) {
		var C = document.getElementsByTagName(B);
		if (C != null) {
			for (var A = 0; A < C.length; A++) {
				if (C[A].style.display != "none" && C[A].style.visibility != "hidden") {
					C[A].style.visibility = "hidden";
					this.someToHidden.push(C[A])
				}
			}
		}
	},
	hiddenEle : function(B) {
		var A = document.getElementById(B);
		if ( typeof (A) != "undefined" && A != null) {
			A.style.visibility = "hidden";
			this.someToHidden.push(A)
		}
	},
	showSome : function() {
		for (var A = 0; A < this.someToHidden.length; A++) {
			this.someToHidden[A].style.visibility = "visible"
		}
		for (var A = 0; A < this.someToDisabled.length; A++) {
			this.someToDisabled[A].disabled = false
		}
	}
};
var Dragdrop = new Class();
Dragdrop.prototype = {
	initialize : function(C, B, A, D, E) {
		this.dragData = null;
		this.dragDataIn = null;
		this.backData = null;
		this.width = C;
		this.height = B;
		this.shadowWidth = A;
		this.showShadow = D;
		this.contentType = E;
		this.IsDraging = false;
		this.oObj = G("dialogBox");
		Event.observe(G("dialogBoxTitle"), "mousedown", this.moveStart.bindAsEventListener(this), false)
	},
	moveStart : function(A) {
		this.IsDraging = true;
		if (this.contentType == 1) {
			G("iframeBG").style.display = "";
			G("iframeBG").style.width = this.width + "px";
			G("iframeBG").style.height = this.height + "px"
		}
		Event.observe(document, "mousemove", this.mousemove.bindAsEventListener(this), false);
		Event.observe(document, "mouseup", this.mouseup.bindAsEventListener(this), false);
		Event.observe(document, "selectstart", this.returnFalse, false);
		this.dragData = {
			x : Event.pointerX(A),
			y : Event.pointerY(A)
		};
		this.backData = {
			x : parseInt(this.oObj.style.left),
			y : parseInt(this.oObj.style.top)
		}
	},
	mousemove : function(A) {
		if (!this.IsDraging) {
			return
		}
		var C = Event.pointerX(A) - this.dragData.x + parseInt(this.oObj.style.left);
		var B = Event.pointerY(A) - this.dragData.y + parseInt(this.oObj.style.top);
		if (this.dragData.y < parseInt(this.oObj.style.top)) {
			B = B - 12
		} else {
			if (this.dragData.y > parseInt(this.oObj.style.top) + 25) {
				B = B + 12
			}
		}
		this.oObj.style.left = C + "px";
		this.oObj.style.top = B + "px";
		if (this.showShadow) {
			G("dialogBoxShadow").style.left = C + this.shadowWidth + "px";
			G("dialogBoxShadow").style.top = B + this.shadowWidth + "px"
		}
		this.dragData = {
			x : Event.pointerX(A),
			y : Event.pointerY(A)
		};
		document.body.style.cursor = "move"
	},
	mouseup : function(C) {
		if (!this.IsDraging) {
			return
		}
		if (this.contentType == 1) {
			G("iframeBG").style.display = "none"
		}
		document.onmousemove = null;
		document.onmouseup = null;
		var B = Event.pointerX(C) - (document.documentElement.scrollLeft || document.body.scrollLeft);
		var A = Event.pointerY(C) - (document.documentElement.scrollTop || document.body.scrollTop);
		if (B < 1 || A < 1 || B > document.body.clientWidth || A > document.body.clientHeight) {
			this.oObj.style.left = this.backData.x + "px";
			this.oObj.style.top = this.backData.y + "px";
			if (this.showShadow) {
				G("dialogBoxShadow").style.left = this.backData.x + this.shadowWidth + "px";
				G("dialogBoxShadow").style.top = this.backData.y + this.shadowWidth + "px"
			}
		}
		this.IsDraging = false;
		document.body.style.cursor = "";
		Event.stopObserving(window, "selectstart", this.returnFalse, false)
	},
	returnFalse : function() {
		return false
	}
};
/**
 *弹出iframe 
 * @param {Object} iframesrc
 * @param {Object} title
 */
function showIframe(iframesrc,title) {
	var pop = new Popup({
		contentType : 1,
		isReloadOnClose : false,
		width : 800,
		height : 400
	});
	pop.setContent("contentUrl", iframesrc);
	pop.setContent("title", title);
	pop.build();
	pop.show();
}
/**
 *弹出iframe 
 * @param {Object} iframesrc
 * @param {Object} title
 */
function showIframe2(iframesrc,title) {
	var pop = new Popup({
		contentType : 1,
		isReloadOnClose : false,
		width : 800,
		height : 510
	});
	pop.setContent("contentUrl", iframesrc);
	pop.setContent("title", title);
	pop.build();
	pop.show();
}
/**
 *关闭当前弹出窗口 
 */
function closeCurrentPop(type){
	var closeBtn;
	if(type){
		closeBtn = window.parent.document.getElementById("dialogBoxClose");
	}else{
		closeBtn = document.getElementById("dialogBoxClose")
	}
	closeBtn.click();
}
/**
 * 弹出alert框
 * @param {} msg
 */
function showAlert(msg){
	var pop = new Popup({
		contentType : 2,
		isReloadOnClose : false,
		width : 200,
		height : 60
	});
	pop.setContent("contentHtml", "<p style='text-align:center;font-size:12px;padding-top:15px;'>"+msg+"</p>");
	pop.setContent("title", "");
	pop.build();
	pop.show();
	setTimeout("closeCurrentPop(0)",1000); 
}
/**
 * 弹出confirm框
 */
function showConfirm(msg){
	var builder = new StringBuilder();
	builder.append('<div style="text-align:center;font-size:12px;padding-top:15px;">');
	builder.append(msg);
	builder.append('<p id="buttons" style="margin-top:40px;">');
	builder.append('<input type="button" hidefocus onclick="con()" id="confirm" value="确认"  style="float:left;margin-left:10px;border:1px solid grey;cursor:pointer;background-color:#709CD2;"/>');
	builder.append('<input type="button" hidefocus onclick="can()" id="cancel" value="取消"  style="margin-left:90px;border:1px solid grey;cursor:pointer;background-color:#709CD2;"/>');
	builder.append('</p>');
	builder.append('</div>');
	var pop = new Popup({
		contentType : 2,
		isReloadOnClose : false,
		width : 200,
		height : 100
	});
	pop.setContent("contentHtml", builder.toString());
	pop.setContent("title", "");
	pop.build();
	pop.show();
}
/**
 * 确认
 */
function confirm(){
	closeCurrentPop(0);
	return true;
}
/**
 * 取消
 */
function cancel(){
	closeCurrentPop(0);
	return false;
}

 

4
1
分享到:
评论

相关推荐

    javascript常用函数 javascript 常用库

    在`common.base.js`这样的文件中,通常会包含一些常见的公共方法,比如通用的DOM操作、数据处理、网络请求封装等。这些方法可以被项目中的其他代码复用,减少代码重复,提高代码质量。例如,可能有一个`addClass`...

    JavaScript 通用库

    JavaScript通用库是编程实践中常用的工具,它集合了一系列实用的函数,可以帮助开发者简化代码,提高开发效率。在Web开发中,JavaScript作为客户端脚本语言,它的通用库尤其重要,因为它们可以处理各种常见的任务,...

    JavaScript中通用的两个非空检测函数

    ### JavaScript中通用的两个非空检测函数 在前端开发领域,特别是使用JavaScript进行表单验证时,非空检测是非常常见的需求之一。对于新手而言,掌握几种简单有效的非空检测方法非常有帮助。本文将详细介绍两种常用...

    js validator通用函数及实例

    “js validator通用函数及实例”这个压缩包是学习和提升JavaScript数据验证能力的好资源,通过研究其中的函数和示例,开发者不仅可以学会基本的验证方法,还能了解到如何将这些方法灵活地应用到实际项目中,...

    js常用函数封装cookiedom操作自适应浏览器前缀函数节流等

    在JavaScript开发中,掌握一些常用的函数封装技巧可以显著提高代码的可维护性和效率。这里我们将深入探讨`cookie`操作、`DOM`操作、浏览器前缀处理以及函数节流技术这四个核心主题,这些都是JavaScript开发中不可或...

    js实现ajax通用函数

    在实际项目中,我们还可以进一步优化这个通用函数,比如添加对其他数据类型的处理(如XML、HTML),支持跨域请求,处理预检请求(CORS),以及添加请求超时等功能。 在提供的压缩包文件中,有一个名为`doi18n.js`的...

    JavaScript 函数库、工具类.zip

    在编程中,工具类通常是一些静态方法的集合,它们提供了一些通用功能,如日期处理、字符串操作、数组操作等,不依赖于实例化对象,可以直接调用。这些工具类能够帮助开发者写出更加简洁、可维护的代码。 XEUtils...

    JavaScript常用函数工具集-lao-utils_.docx

    `lao-utils` 是一个JavaScript函数工具库,它提供了一系列常用的方法,用于简化常见的编程任务。这个库旨在作为其他大型库如jQuery、YUI等的补充,提供了许多实用的工具函数,适用于Node.js环境和浏览器环境。以下是...

    JS代码重构:避免冗余,提取通用函数(1)

    【JavaScript 代码重构:避免冗余,提取通用函数(1)】 在JavaScript编程中,代码冗余是一个常见的问题,它不仅降低了代码的可读性和可维护性,还可能导致额外的bug。本文旨在介绍如何通过重构技术来消除重复的...

    JavaScript 通用库(一)

    JavaScript 通用库是一种为了简化开发工作,提高代码复用率而创建的集合,它包含了一系列常用的函数或方法,便于在不同的项目中应用。本篇主要介绍一个名为 `Common.js` 的 JavaScript 类库,该库提供了多种实用的...

    用html5+css+javascript完成的博客项目,涵盖常用函数封装

    "前端-分享侧栏Base.js"可能是基础的脚本文件,包含了通用函数和变量,而"前端-分享侧栏.js"可能是针对特定侧栏功能的扩展或应用。例如,可能包含有实现下拉菜单、轮播图或者拖放功能的代码,如"java-drag.js"所示,...

    一个用于对象深度克隆的同构和可配置javascript函数

    标题提到的"一个用于对象深度克隆的同构和可配置javascript函数"是指一个能够在浏览器环境和Node.js环境中通用(同构)的JavaScript函数,它提供了一种灵活的方式来深度复制复杂的数据结构,同时允许用户根据需求...

    常用的JS验证和函数汇总

    标题“常用的JS验证和函数汇总”表明了本文将要分享的是在JavaScript(JS)编程中常用的验证和函数集。这些验证和函数是作者在日常工作或项目中积累下来的,具有实用价值,并且能够被广泛应用。从描述中可以看出,...

    javascript 的基本上所有的常用类库

    本文将详细介绍JavaScript的一些常用类库,这些类库极大地扩展了JavaScript的功能,并且在实际开发中非常常见。 1. **jQuery**:jQuery是JavaScript中最著名的类库之一,它简化了DOM操作、事件处理、动画效果和Ajax...

    Web客户端统一验证JavaScript函数库组件概述Web客户端统一验证JavaScript函数库组件概述Web客户端统一验证JavaScript函数库组件概述Web客户端统一验证JavaScript函数库组件概述

    JavaScript函数库的设计通常遵循模块化和可复用性原则,使得开发者可以方便地在不同项目中引入和使用。例如,ChkInputs这个文件可能是该函数库的核心实现,包含了各种验证函数和相关的配置选项。它可能包含以下功能...

    项目javascript

    在“项目javascript”这个主题中,我们可以探讨一系列与JavaScript编程相关的知识,这些知识在实际的项目开发中至关重要。JavaScript是一种广泛应用于网页和网络应用的脚本语言,它为客户端交互提供了强大的支持,...

    MyUtils:常用JavaScript工具函数

    除了上述功能,MyUtils还包含了一些通用的辅助函数,如`randomId`生成随机ID,`debounce`和`throttle`用于节流和防抖,提高事件处理的性能,以及`ajax`封装了异步请求,简化了网络通信的操作。 MyUtils库持续更新中...

    JavaScript编码通用规范-顺丰

    以上仅为JavaScript编码通用规范的一部分,实践中还需要结合具体项目和团队习惯进行调整。遵循这些规范,可以显著提高代码质量和团队协作效率。对于顺丰这样的企业来说,这样的规范对于构建稳定、高效、易于维护的...

    utils:前端常用的一些公共函数

    这个"utils:前端常用的一些公共函数"可能就是一个这样的库,专门收集并封装了JavaScript中常见的实用函数。 在JavaScript中,公共函数库可能包括但不限于以下几个方面的功能: 1. 数组操作:如`arrayMap`用于对数...

    100多个基础常用JS函数和各种数据转换处理集合大全.zip

    5. **类型检查与转换**:JavaScript的动态类型可能导致类型错误,资源包中可能提供了一些判断数据类型的函数,如`isString`、`isNumber`等,以及类型转换的辅助函数。 6. **DOM操作**:对于Web开发,操作文档对象...

Global site tag (gtag.js) - Google Analytics