/*
 * 查询字符串加密,服务端仅解密一次
 */
function uriEncode(param){
	return encodeURI(encodeURI(param));
}
/*
 * 查询字符串解密
 */
function uriDecode(param){
	return decodeURI(decodeURI(param));
}
/*
 * 输入框 特殊限制 仅限数字
 */
function limitNumber(obj){
	obj.value=obj.value.replace(/[\D]/g,'');
}
/*
 * 输入框 特殊限制 仅限字母数字下划线
 */
function limitNoOther(obj){
	obj.value=obj.value.replace(/[\W]/g,'');
}
/*
 * 输入框 特殊限制 仅限非英文,特殊字符
 */
function limitOther(obj){
	obj.value=obj.value.replace(/[^\W]/g,'');
}
/*
 * 输入框 特殊限制 仅限非数字
 */
function limitNaN(obj){
	obj.value=obj.value.replace(/[^\D]/g,'');
}
/*
 * 限定长度
 */
function limitLength(obj,maxLength){
	obj.value=obj.value.substring(0,maxLength);
}
/*
 * 限制 窗口大小
 */
function keepWidth(width,height){
	var obody=document.body;
	if(obody.clientWidth<width){
		window.resizeTo(width,height);
	}
}
/*
 * 限制 顶层窗口
 */
function lonely(){
	if(top!=self){
		top.location.href=window.location.href;
	}
}
/*
 * 字符串操作去空格
 */
String.prototype.Trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.LTrim   =   function(){
	return this.replace(/(^\s*)/g,   "");
}
String.prototype.RTrim   =   function(){
	return this.replace(/(\s*$)/g,   "");
}
/**
 * DIV滚动方法(obj,scrollAble,outlineAble,tobj)
 * 1.div对象id
 * 2.是否随屏滚动(0,1)[false,true]
 * 3.是否可以出界(0,1)[false,true]
 * 4.控制div的 title对象[一般为表头或tr]
 */
function simpleDrag(obj,scrollAble,outlineAble,tobj){
	if(!obj || typeof obj == "undefined"){
		return;	
	}
	var oZIndex=0;
	var oldX,oldY;//老的 clientX,clientY
	var oldLeft,oldTop;	//老的pixelLeft,pixelTop
	var scrollX,scrollY;//老的 左边距
	var isMove=false;
	if(typeof(tobj)=="undefined"||!tobj){
		tobj=obj;
	}
	tobj.onmouseup=function(){clear();}
	tobj.onmousedown=function(){init();}
	var init = function(){ 	//准备移动
		/*移动的时候是否可选中内容*/
		oZIndex=obj.style.zIndex;
		obj.style.zIndex=10000;
		oldX=EV.clientX();
		oldY=EV.clientY();
		var left=obj.style.left;
		var top=obj.style.top;
		oldLeft=parseInt(left?left:0);
		oldTop=parseInt(top?top:0); 
		tobj.style.cursor="move";
		document.onmousemove=function(){move();}
	}
	/*开始移动*/
	var move = function(){
		tobj.setCapture?tobj.setCapture(true) : document.captureEvents(EV.MOUSEDOWN);
		isMove=false;
		if(EV.button()==1){
			isMove=true;	
		}
		if(isMove){
			try{
				obj.style.left=(oldLeft+EV.clientX()-oldX)+"px";
   				obj.style.top=(oldTop+EV.clientY()-oldY)+"px";
   			}catch(e){}
   			scrollX=parseInt(obj.style.left)-parseInt(document.body.scrollLeft);
   			scrollY=parseInt(obj.style.top)-parseInt(document.body.scrollTop);
   			if(outlineAble){//不出界
   				var pl=obj.style.left;
   				var pt=obj.style.top;
   				if(pl<=0){
   					obj.style.left=0;	
   				}
   				if(pt<=0){
   					obj.style.top=0;	
   				}
   			}
		}
	}
	/*清场*/
	var clear=function(){
		tobj.releaseCapture?tobj.releaseCapture() : document.releaseEvents(EV.MOUSEDOWN);   
		document.onmousemove=function(){return false};
		tobj.onmousemove=function(){return false};
		obj.style.zIndex=oZIndex;
		obj.style.cursor="normal";
		EV.stopPropagation();
	}
	if(scrollAble){
		window.onscroll=function(){
		obj.style.left=scrollX+document.body.scrollLeft;
		obj.style.top=scrollY+document.body.scrollTop;
	}	
}
	}
/*
 * event函数集合
 * @class event对象
 */
function EV(){}
	EV.getTarget = fGetTarget;//获取target,获取 事件源 ie:srcElement,firefox:target
	EV.getEvent = fGetEvent;//获取event
	EV.stopEvent = fStopEvent;//取消事件和事件冒泡
	EV.stopPropagation = fStopPropagation;//取消事件冒泡
	EV.preventDefault = fPreventDefault;//取消事件
	EV.button= fGetButton;//获取点击的button
	EV.addEvent=function(obj,sEvent,fpNotify){
		if(obj.addEventListener){
			sEvent=sEvent.substring(sEvent.indexOf("on")+2);
			obj.addEventListener(sEvent,fpNotify,false);
		}else{
			obj.attachEvent(sEvent,fpNotify);
		}
	}
	/*移除事件*/
	EV.removeEvent=function(obj,sEvent,fpNotify){
		if(obj.removeEventListener){
			sEvent=sEvent.substring(sEvent.indexOf("on")+2)
			obj.removeEventListener(sEvent,fpNotify,false);
		}else{
			obj.detachEvent(sEvent,fpNotify);
		}
	}
	/*获取点击位置*/
	EV.clientX=function(){
		var ev=this.getEvent();
		var x=ev.clientX || ev.pageX;
		return parseInt(x);
	}
	/*获取点击位置*/
	EV.clientY=function(){
		var ev=this.getEvent();
		var y=ev.clientY || ev.pageY;
		return parseInt(y);
	}
/*获取点击的button*/	
function fGetButton(ev){
	if(!ev) ev = this.getEvent();
	if(!ev.which&&ev.button){//ie
		return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
	}
	return ev.which;
}
/*获取target,获取 事件源 ie:srcElement,firefox:target*/
function fGetTarget(ev, resolveTextNode){
	if(!ev) ev = this.getEvent();
    var t = ev.target || ev.srcElement;
    if (resolveTextNode && t && "#text" == t.nodeName) {
	    return t.parentNode;
    }else{
    	return t;
    }
}
/*获取event*/
function fGetEvent(e){
	var ev = e || window.event;
    if (!ev) {
		var c = this.getEvent.caller;
        while (c) {
        	ev = c.arguments[0];
            if (ev && Event == ev.constructor) {
            	break;
            }
            c = c.caller;
		}
	}
    return ev;
}
/*取消事件和事件冒泡*/
function fStopEvent(ev) {
     if(!ev) ev = this.getEvent();
     this.stopPropagation(ev);
     this.preventDefault(ev);
}
/*取消事件冒泡*/
function fStopPropagation(ev) {
     if(!ev) ev = this.getEvent();
     if (ev.stopPropagation) {
           ev.stopPropagation();
     } else {
           ev.cancelBubble = true;
     }
}
/*取消事件*/
function fPreventDefault(ev) {
     if(!ev) ev = this.getEvent();
     if (ev.preventDefault) {
           ev.preventDefault();
     } else {
           ev.returnValue = false;
     }
}
/*
 * 日历 返回数据格式如:0909
 */
function showCalendar(){
	var value= window.showModalDialog("../rili.htm","","dialogHeight:220px;dialogWidth:192px");
	if(typeof(value)=="undefined"){
		var date=new Date();
		var month=(date.getMonth()+1);
		var day=date.getDate();
		day=day.length==1?"0"+day:day;
		value=month+""+day;
		value=value.length==3?"0"+value:value;
	}
	return value;
}
/*
 * div日历,兼容ie,firefox
 */
function showCalendarD(layer,inputID){
	if(layer.style.display="none")
		layer.style.display="";
	else
		layer.style.display="none";
	window.dateFrame.document.getElementById("layer").value=inputID;
}
/*
 * 根据字符串获取json,str内部字符串不能有双引号
 */
function getJson(str){
	return eval("("+str+")");
}
/*
 * 全选ByName
 */
function chooseAll(name){
	var objs=document.getElementsByName(name);
	if(event.srcElement.checked){
		for(i=0;i<objs.length;i++){
			if(objs[i].disabled==false)
				objs[i].checked=true;
		}
	}else{
		for(i=0;i<objs.length;i++){
			if(objs[i].disabled==false)
				objs[i].checked=false;
		}
	}
}
/*
 * 提示出错信息
 */
function showAlert(span,info){
	document.getElementById(span).innerHTML=info;
}
/*
 * 不提示出错信息
 */
function hiddenAlert(span){
	document.getElementById(span).innerHTML="";
}
/*
 * 替换全部
 */
function ReplaceAll(strOrg, strFind, strReplace) {
	var index = 0;
	while (strOrg.indexOf(strFind, index) != -1) {
		strOrg = strOrg.replace(strFind, strReplace);
		index = strOrg.indexOf(strFind, index);
	}
	return strOrg;
}
/*
 * 根据id获取对象
 */
function $(id){return document.getElementById(id);}
/* 
 * 根据name获取对象数组
 * ie8,只可通过name中表单元素,非表单找不到
 */
function $$(name){return document.getElementsByName(name);}
/*
 *根据标签名获得对象 
 */
function $$$(tagName){return document.getElementsByTagName(tagName);}
/*
 * 控制页面中下拉框是否显示
 */
function ctrlSelect(isShow){
	var ss = $$$("select");
	for(var i=0;i<ss.length;i++){
		var one = ss[i];
		if(false == isShow){
			one.style.visibility = "hidden";	
		}else{
			one.style.visibility = "visible";
		}
	}
}
  详细解决方案
                惯用js总结
热度:74   发布时间:2012-10-27 10:42:26.0
                    相关解决方案