日期:2014-05-16  浏览次数:20494 次

常用js总结


/*
* 查询字符串加密,服务端仅解密一次
*/
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.preventDef