// collected by 钱途无梁  (2009-01-10)
/*-------------封装开始------------------*/
function GetId(id){return document.getElementById(id)}  //通过ID获取对象
function GetTag(tag){return document.getElementsByTagName(tag)}  //通过Tag获取对象(html标签名称)
function GetChildTag(id,tag){return id.getElementsByTagName(tag)}  //通过Tag获取某ID对象的子对象
function GetClass(className){return getElementsByClassName(className)}  //通过className获取对象
var $c=function(array){var nArray = [];for (var i=0;i<array.length;i++) nArray.push(array[i]);return nArray;};
    Array.prototype.each=function(func){for(var i=0,l=this.length;i<l;i++) {func(this[i],i);};};
var getElementsByClassName=function(cn){
        var hasClass=function(w,Name){
        var hasClass = false;
        w.className.split(' ').each(function(s){
            if (s == Name) hasClass = true;
        });
        return hasClass;
            };
        var elems =document.getElementsByTagName("*")||document.all;
            var elemList = [];
           $c(elems).each(function(e){
               if(hasClass(e,cn)){elemList.push(e);}
           })
            return $c(elemList);
    };  
   
/*-----------------封装结束---------------------*/
//---------------ajax_func:start---------------------------------------------------------------
var http_request = false;
function send_request(method,url,content,responseType,callback) {
 http_request=false;
 if(window.XMLHttpRequest)
 {//Mozilla浏览器
  http_request=new XMLHttpRequest();
  if(http_request.overrideMimeType)
  {
   http_request.overrideMimeType("text/xml");
  }
 }
 else
 {
  try
  {
   http_request=new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e)
  {
   try
   {
    http_request=new ActiveXObject("Microsoft.XMLHTTP");
   }catch(e)
   {}
  }
 }
 if(!http_request)
 {
  window.alert("不能创建XMLHttpRequest对象实例。");
  return false;
 }
 if(responseType.toLowerCase()=="text")
 {
  http_request.onreadystatechange=callback;
 }
 else
 {
  
  window.alert("响应类别参数错误。");
  return false;
 }
 if(method.toLowerCase()=="get")
 {
  http_request.open(method,url,true);
 }
 else if(method.toLowerCase()=="post")
 {
  http_request.open(method,url,true);
  http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 }
 else
 {
  window.alert("http请求类别参数错误。");
  return false;
 }
 http_request.send(content);
 
}
//处理返回文本格式信息的函数
function processTextResponse()
{
 if(http_request.readyState==4)
 {
  if(http_request.status==200)
  {
   alert("Text文档响应");
  }else
  {
   alert("您所请求的页面有异常。");
  }
 }
}
function processXMLResponse()
{
 if(http_request.readyState==4)
 {
  if(http_request.status==200)
  {
   alert("XML文档响应");
  }
  else
  {
   alert("您所请求的页面有异常。");
  }
 }
}
//eg.
 /*  function init_issuance_edit()
   {
      var allCheckedResInfo=document.getElementById("allCheckedResInfo");
     
      var url="js/wjb/user_publish_ajax1.aspx?para="+Math.round(Math.random()*10000)+"&allCheckedResInfo="+escape(allCheckedResInfo.value);
      send_request("GET",url,null,"text",searchRes_callback);
      //创建搜索资源的回调函数
      function searchRes_callback()
      {
        if(http_request.readyState==4)//服务器响应状态
        {
            if(http_request.status==200|| http_request.status == 304)//代码执行状态
            {
                var resStr=http_request.responseText;
                alert(resStr);//这里再处业务逻辑处理
           }
          else
            {
             alert("您所请求的页面有异常!");
            }
        }
        else
        {
         selectedResTits.innerHTML="<img src='images/wjb/icons/indicator.gif' alt=''/>";
        }
      
      }
   }
 
  */
//      protected void Page_Load(object sender, EventArgs e)
//    {
//        string strOut="123";
//        Response.ContentType = "text/plain";
//        if (Request.QueryString["strUserName"] != null && Request.QueryString["strUserName"] != "")
//        {
//            strUserName = Request.QueryString["strUserName"].ToString().Trim();
//        }
//        else
//        {
//            strUserName = "";
//        }
//        Response.Write(strOut);
//        Response.End();
//    }
//---------------ajax_func:end---------------------------------------------------------------
//---------------移动弹出层:---------------------------------------------------------------
function move(element,event)
{
 var x=parseInt(element.style.left);
 var y=parseInt(element.style.top);

 var deltaX=event.clientX-x;
 var deltaY=event.clientY-y;
 //添加临时事件
 if(document.addEventListener){//2级DOM
  document.addEventListener("mousemove",moveHandler,true);
  document.addEventListener("mouseup",upHandler,true);
 }else if(document.attachEvent){//IE5+
  document.attachEvent("onmousemove",moveHandler);
  document.attachEvent("onmouseup",upHandler);
 }else{//IE4
  var oldonmousemove=document.onmousemove;
  var oldonmouseup=document.onmouseup;
  document.onmousemove=moveHandler;
  document.onmouseup=upHandler;
 }
 //我们处理该事件,不让其他元素见到它
 if(event.preventDefault){//2级DOM
  event.preventDefault();
 }else{//IE
  event.returnValue=false;
 }

 //鼠标移动事件
 function moveHandler(e){
  if(!e){//IE
   e=window.event;
  }
  
        var testa=(e.clientX-deltaX)+"px";
  element.style.left=(e.clientX-deltaX)+"px";
  element.style.top=(e.clientY-deltaY)+"px";
  var left=parseInt(element.style.left);
  var top=parseInt(element.style.top);
  if(e.stopPropagation){//2级DOM
   e.stopPropagation();
  }else{//IE
   e.cancelBubble=true;
  }
 }
 //鼠标按键弹起
 function upHandler(e){
  if(!e){
   e=window.event;
  }
  if(document.removeEventListener){//2级DOM
   document.removeEventListener("mouseup",upHandler,true);
   document.removeEventListener("mousemove",moveHandler,true);
  }else if(document.detachEvent){//IE5+
   document.detachEvent("onmouseup",upHandler);
   document.detachEvent("onmousemove",moveHandler);
  }else{//IE4
   document.onmouseup=oldonmouseup;
   document.onmousemove=oldonmousemove;
  }
  if(e.stopPropagation){//2级DOM
   e.stopPropagation();
  }else{//IE
   e.cancelBubble=true;
  }
 }
}

//
//去左空格;
function ltrim(s)
{
  return s.replace( /^\s*/, "");
}
//去右空格;
function rtrim(s)
{
  return s.replace( /\s*$/, "");
}
//去左右空格;
function trim(s)
{
  return rtrim(ltrim(s));
}


//截取字符串 包含中文处理
//(串,长度,增加...)

function CutString(str, len, hasDot)
{
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex,"**").length;
    for(var i = 0;i < strLength;i++)
    {
      singleChar = str.charAt(i).toString();
      if(singleChar.match(chineseRegex) != null)
    {
      newLength += 2;
    }
    else
    {
       newLength++;
    }
    if(newLength > len)
    {
      break;
    }
      newStr += singleChar;
    }
    if(hasDot && strLength > len)
    {
      newStr += "...";
    }
    return newStr;
}

function singleToDouble(obj)
{
 var tmpStr=obj;
 tmpStr=tmpStr.replace(",",",");
 tmpStr=tmpStr.replace("'",",");
 return tmpStr;
}
function doubleToSingle(obj)
{
  var tmpStr=obj;
  tmpStr=tmpStr.replace(",",",");
  return tmpStr;
}
//---------------js各种类型验证:---------------------------------------------------------------
function chkEmail(obj)
{
 var patrn= /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    var emailObj=obj;
 if (!patrn.exec(emailObj))
     {
        return 0;  //表示验证失败
     }
  return 1; //表示验证成功
}
//测试字符串的长度:
function chkStrLen(str)
 {
    return str.replace(/[^\x00-\xFF]/g,'**').length;

}
//Js验证是否为数字
function checknumber(String) {
    var Letters = "1234567890";
    var i;
    var c;
    for (i = 0; i < String.length; i++) {
        c = String.charAt(i);
        if (Letters.indexOf(c) == -1) {
            return true;
        }
    }
    return false;
}
//javascript中获取地址栏参数
 function getValue(name) {
     var tmpName = name
     var URLParams = new Array();
     var aParams = document.location.search.substr(1).split('&'); //是个数组
     for (i = 0; i < aParams.length; i++) {
         var aParam = aParams[i].split('='); //是个数组
         URLParams[aParam[0]] = aParam[1];
     }
     return (URLParams[tmpName]);
 }

posted on 2009-03-13 12:00  钱途无梁  阅读(533)  评论(0编辑  收藏  举报