你的位置:首页 > Java教程

[Java教程]javascript几个常用静态方法


util=function(){  return {    $:function(id){      return document.getElementById(id);    },    trim:function(str){      return str.replace(/(^\s+)|(\s+$)/g, "");    },    len:function(str){        return str.replace(/[^\x00-\xff]/g,'**').length;      },    format:function(str){      var arg = arguments;      return str.replace(/\{(\d+)\}/g, function(m, i){        return arg[parseInt(i)+1];      });    },    each:function(object, callback, args){      var name, i = 0, length = object.length;      if ( args ) {        if ( length === undefined ) {          for ( name in object )            if ( callback.apply( object[ name ], args ) === false )              break;        } else          for ( ; i < length; )            if ( callback.apply( object[ i++ ], args ) === false )              break;      } else {        if ( length === undefined ) {          for ( name in object )            if ( callback.call( object[ name ], name, object[ name ] ) === false )              break;        } else          for ( var value = object[0];            i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}      }    },    setCookie:function(name,value,hours,path,domain){      var str=new String();      var nextTime=new Date();      nextTime.setHours(nextTime.getHours()+hours);      str=name+"="+escape(value);      if(hours)        str+=";expires="+nextTime.toGMTString();      if(path)        str+=";path="+path;      if(domain)        str+=";domain="+domain;      document.cookie=str;    },    getCookie:function(name){      var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;      if(tmp=rs)        return unescape(tmp[2]);      return null;    },    delCookie:function(name){      document.cookie = name + "=-1" + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";    },    /**    *url String    *parms String    *method String default value "get"    *asy Boolean defalut value true    *success Function(http_request.responseText)    **/    ajax:function(config){      var url=config.url,        parms=(config.parms?config.parms:"") + "&t="+new Date().getTime(),        method=config.method||"get",        asy=true;      var http_request=null;      if(method.toLowerCase()=="get"){        url=url+"?"+parms;        parms=null;      }      //开始初始化