栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

JavaScript 应用类库代码

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript 应用类库代码





function $(element) {
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($(arguments[i]));
    return elements;
  }
  if (typeof element == 'string')
    element = document.getElementById(element);
  return element;
}

/// 浏览器相关操作
var Sams_browse = {

checkBrowser : function ()

    this.ver=navigator.appVersion 
    this.dom=document.getElementById?1:0 
    this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0; 
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
    this.ns4=(document.layers && !this.dom)?1:0; 
    this.mac=(this.ver.indexOf('Mac') > -1) ?1:0; 
    this.ope=(navigator.userAgent.indexOf('Opera')>-1); 
    this.ie=(this.ie6 || this.ie5 || this.ie4) 
    this.ns=(this.ns4 || this.ns5) 
    this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope) 
    this.nbw=(!this.bw) 
    return this;
},


SetDefault : function ()
{
  this.style.behavior='url(#default#homepage)';
  this.setHomePage(this.GetUrl());
  return false;
},


SetCopy : function (Msg){
  if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
   clipboardData.setData('Text',Msg);
   alert ("网址“"+Msg+"”n已经复制到您的剪贴板中n您可以使用Ctrl+V快捷键粘贴到需要的地方");
  }
  else 
  {
   prompt("请复制网站地址:",Msg); 
  }
},


AddBookmark : function (site, url){
  if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
   window.external.addFavorite(url,site)
  } else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
   alert ("请使用Ctrl+T将本页加入收藏夹");
  } else {
   alert ("请使用Ctrl+D将本页加入收藏夹");
  }
},


OpenWindows : function (url,width,height)
{
  window.open(url,'newwin','width='+width+',height='+height);
  return false;
},


CloseError : function(){
  window.onerror = function(){return true;};
},


GetUrl : function(){
  return location.href;
},


GetUrlParam : function(){
  return location.search;
},


GetFrom : function(){
  return document.referrer;
},


Request : function(name){
  var GetUrl = this.GetUrl();
  var Plist = new Array();
  if(GetUrl.indexOf('?') > 0)
  {
   Plist = GetUrl.split('?')[1].split('&');
  }
  else if(GetUrl.indexOf('#') > 0)
  {
   Plist = GetUrl.split('#')[1].split('&');
  }
  if (GetUrl.length > 0)
  {
   for(var i=0; i   {
    var GetValue = Plist[i].split('=');
    if (GetValue[0].toUpperCase() == name.toUpperCase())
    {
     return GetValue[1];
     break;
    }
   }
   return;
  }
},


Popmsg : function PopIt(title,msg)
{
  var popup = window.open('','popDialog','height=500,width=400,scrollbars=yes');
  popup.document.write(''+title+'body{margin:10px;font:13px Arial;}span{text-line:20px;}'+msg + '');
  popup.document.close();
}
};


/// 对象操作
var Sams_object = {


CreateDiv : function (ID,ClassName,SetValue,ToDiv){
  var creatediv = document.createElement('div');
  if(ID != null) creatediv.id = ID;
  creatediv.style.position = 'absolute';
  if(ClassName != null) creatediv.className = ClassName;
  if(this.Get(ToDiv))
  {
   this.Get(ToDiv).appendChild(creatediv);
  }
  else
  {
   document.getElementsByTagName('body')[0].appendChild(creatediv);
  }
  this.SetValue(ID,SetValue);
  return this.Get(ID);
},


DeleteDiv : function (objid)
{
  try
  {
   if(this.Get(objid))
   {
    var GetParent = this.Get(objid).parentNode;
    GetParent.removeChild(this.Get(objid));
    return true;
   }
   else
   {
    return false;
   }
  }
  catch(e)
  {
   return false;
  }
},


Get : function (objid) {
  if (arguments.length > 1) {
   for (var i = 0, objids = [], length = arguments.length; i < length; i++)
     objids.push(this.Get(arguments[i]));
   return objids;
  }
  if (typeof objid == 'string')
  {
   if (document.getElementById) {
    objid = document.getElementById(objid);
   } else if (document.all) {
    objid = document.all[objid];
   } else if (document.layers) {
    objid = document.layers[objid];
   }
  }
  return objid;
},


GetValue : function (objid) {
  if (typeof objid == 'string')
  {
   var getTagName = this.Get(objid).tagName.toLowerCase();
   if (getTagName == 'input' || getTagName == 'textarea' || getTagName == 'select')
   {
    return this.Get(objid).value;
   }
   else if (getTagName == 'div' || getTagName == 'span')
   {
    return this.Get(objid).innerText;
   }
  }
  else if (typeof objid == 'object')
  {
   return objid.value;
  }
},


SetValue: function(objid,inserValue) {
  var getTagName = this.Get(objid).tagName.toLowerCase();
  if (inserValue == null) inserValue = '';
  if (getTagName == 'input' || getTagName == 'textarea')
  {
   this.Get(objid).value = inserValue;
  }
  else if (getTagName == 'div' || getTagName == 'sapn')
  {

   this.Get(objid).innerText = inserValue;
  }
},


CopyCode : function (str) {
  var rng = document.body.createTextRange();
  rng.moveToElementText(str);
  rng.scrollIntoView();
  rng.select();
  rng.execCommand("Copy");
  rng.collapse(false);
},


ShowHidd : function(objid,isshow){
  if (isshow != null)
  {
   if(isshow)
   {
    this.Get(objid).style.display = 'block';
   }
   else
   {
    this.Get(objid).style.display = 'none';
   }
  }
  else
  {
   if(this.Get(objid).style.display == 'none')
   {
    this.Get(objid).style.display = 'block';
   }
   else
   {
    this.Get(objid).style.display = 'none';
   }
  }
},


IsVisible : function(objid){
  if(this.Get(objid))
  {
   try
   {
    if (this.Get(objid).style.display == 'none')
    {
     return false
    }
    if(this.Get(objid).style.visibility == 'hidden')
    {
     return false;
    }
    return true;
   }
   catch(e)
   {
    return false;
   }
  }
  else
  {
   return false;
  }
}
};

/// 字符处理
var Sams_string = {

Left : function (str,n)
{
  if(str.length > 0)
  {
   if(n>str.length) n = str.length;
   return str.substr(0,n)
  }
  else
  {
   return;
  }
},


Right : function (str,n)
{
  if(str.length > 0)
  {
   if(n>=str.length) return str;
   return str.substr(str.length-n,n);
  }
  else
  {
   return;
  }
},


Trim : function (str) 
{
  if (typeof str == 'string') return str.replace(/(^s*)|(s*$)/g, '');
},


Ltrim : function (str) 

  if (typeof str == 'string') return str.replace(/(^s*)/g, '');
},


Rtrim : function (str) 

  if (typeof str == 'string') return str.replace(/(s*$)/g, '');
},


strip : function(str) {
  if (typeof str == 'string') return str.replace(/^s+/, '').replace(/(^s*)|(s*$)/g, '');
},


stripTags : function(str) {
  if (typeof str == 'string')return str.replace(/]+>/gi, '').replace(/(^s*)|(s*$)/g, '');
}
};

/// 时间相关操作
var Sams_time = {

GetDateNow : function (){
  var d,y,m,dd;
  d = new Date(); 
  y = d.getYear();
  m = d.getMonth() + 1;
  dd = d.getDate();            
  return y+"-"+m+"-"+dd;
},

 
AddDays : function(toDate,N){
  var aDate=this._cvtISOToDate(toDate);
  if (!aDate) return "";
  var millis=86400000 * N;
  aDate=new Date(aDate.getTime()+millis);
  return this._fmtDateISO(aDate);
},
_fmtDateISO : function (aDate) {
  with (aDate) {
   var mm=getMonth()+1;
   if (mm<10) {mm='0'+mm;}
   var dd=getDate();
   if (dd<10) {dd='0'+dd;}
   return (getFullYear() + '-' + mm + '-' + dd);
  }
},
_cvtISOToDate : function (isoDate) {
  var atomDate= isoDate.split('-');
  var aDate=new Date(parseInt(atomDate[0],10),parseInt(atomDate[1],10)-1,parseInt(atomDate[2],10),6,0,0);
  return aDate;
}
};

/// 图像相关操作
var Sams_media = {

ZoomFun : function(objid){
  Sams_object.Get(objid).onmousewheel = function(){return Sams_media.imagecontrol(this);}
},

 
ResizeImage: function (IntSize) {
  var imgsinlog=document.getElementsByTagName('img');
  for(j=0; j   if (imgsinlog[j].width >= IntSize) {
    imgsinlog[j].width = IntSize;
    imgsinlog[j].style.cursor= 'pointer';
    imgsinlog[j].onclick = function() {window.open(this.src);}
    if (navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
     imgsinlog[j].title = '您可以用鼠标中键或者使用Ctrl+鼠标滚轮缩放图片,点击图片可在新窗口打开';
     imgsinlog[j].onmousewheel = function(){return Sams_media.imagecontrol(this);};
    }
    else
    {
     imgsinlog[j].title = '点击图片可在新窗口打开';
    }
   }
  }
},
imagecontrol : function(obj){
  var zoom=parseInt(obj.style.zoom, 10)||100;zoom+=event.wheelDelta/12;
  if (zoom>0) obj.style.zoom=zoom+'%';
  return false;
},

 
ImagesError : function(errimgpath){
  var imglist = document.getElementsByTagName('img');
  for(j=0; j   imglist[j].onerror = function(){
    this.src = errimgpath;
   }
  }
},


ShowMedia : function (mFile, mFileType, ObjID, mWidth, mHeight) {
  var mediaStr;
  switch(mFileType){
   case "swf":
    mediaStr="";
    break;
   default :
    mediaStr="";
  }

  var mediaDiv = Sams_object.Get(ObjID);

  if (mediaDiv) {
   mediaDiv.innerHTML = mediaStr;
  } 
  else
  {
   mediaDiv = document.createElement("div");
   mediaDiv.id = ObjID;
   mediaDiv.innerHTML = mediaStr;
   document.getElementsByTagName('body')[0].appendChild(mediaDiv);
  }
  return false;
}
};

/// 样式相关操作
var Sams_style = {

doZoom : function (objid,size){
  Sams_object.Get(objid).style.fontSize=size+'px';
},


ClassName: function(objid, className) {
  Sams_object.Get(objid).className = className;
},


GotoXY : function (obj) {
  var t=obj.offsetTop; 
  var l=obj.offsetLeft; 
  while(obj=obj.offsetParent){ 
   t+=obj.offsetTop; 
   l+=obj.offsetLeft; 
  }
  return Array(t,l);
}
};

/// 科学计算
var Sams_account = {

GetTen: function (i)
{
  var items_One,Get_One;
  if (i.length > 1&& (/^d+$/.test(i)))
  {
     items_One = i.substr(0,i.length-1);
     Get_One = i.substr(i.length-1,1);
     if (parseInt(Get_One)>0)
     {
   items_One = parseInt(items_One)+1;
   items_One = items_One + '0';
     }
     else
   {
   items_One = items_One + '0'; 
   }
  }
  else
  {
   items_One = i;
  }
  return items_One;
}
};

/// 数据验证(所有数值返回值均为Bool型)
var Sams_validate = {

IsNumber : function(str){
  if (/^d+$/.test(str)){return true;}else{return false;}
},


IsNumberObj : function(objid){
  return this.IsNumber(Sams_object.GetValue(objid));
},


IsInt : function(str){
  if (/^(+|-)?d+$/.test(str)){return true;}else{return false;}
},


IsIntObj : function(objid){
  return this.IsInt(Sams_object.GetValue(objid));
},


IsChinese : function(str)
{
  if (/^[u4e00-u9fa5]+$/.test(str)){return true;}else{return false;}
},


IsChineseObj : function(objid)
{
  return this.IsChinese(Sams_object.GetValue(objid));
},


IsLower : function(str)
{
   if (/^[A-Za-z]+$/.test(str)){return true}else{return false;}
},


IsLowerObj : function(objid)
{
   return this.IsLower(Sams_object.GetValue(objid));
},


IsUrl : function(str)
{
  var myReg = /^((http:[/][/])?w+([.]w+|[/]w*)*)?$/;    
  if(myReg.test(str)){return true;}else{return false;}
},


IsUrlObj : function(objid)
{
  return this.IsUrl(Sams_object.GetValue(objid));
},


IsEmail : function(str)
{
  var myReg = /^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+.)+[A-Za-z0-9]{2,3}$/;    
  if(myReg.test(str)){return true;}else{return false;}
},



IsEmailObj : function(objid)
{
  return this.IsEmail(Sams_object.GetValue(objid));
},


IsMobile : function(str)
{
  var regu =/(^[1][3][0-9]{9}$)|(^0[1][3][0-9]{9}$)/;   
  var re = new RegExp(regu);   
  if (re.test(str)){return true;}else{return false;}
},


IsMobileObj : function(objid)
{
  return this.IsMobile(Sams_object.GetValue(objid));
}
};


var Sams_ajax = {
    _objPool: [],
    _getInstance: function (){
        for (var i = 0; i < this._objPool.length; i ++){
            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4){
                return this._objPool[i];
            }
        }
        this._objPool[this._objPool.length] = this._createObj();
        return this._objPool[this._objPool.length - 1];
    },
  _createObj : function (){
        if (window.XMLHttpRequest){
            var objXMLHttp = new XMLHttpRequest();
        }
        else{
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++){
                try{
                    var objXMLHttp = new ActiveXObject(MSXML[n]);
                    break;
                }
                catch(e){
                }
            }
         }          
        if (objXMLHttp.readyState == null){
            objXMLHttp.readyState = 0;
            objXMLHttp.addEventListener("load", function (){
                    objXMLHttp.readyState = 4;
                    if (typeof objXMLHttp.onreadystatechange == "function"){
                        objXMLHttp.onreadystatechange();
                    }
                },  false);
        }
        return objXMLHttp;
    },

/// 开始发送请求
    SendRequest : function (method, url, data, callback,funparam,funparam2){
        var objXMLHttp = this._getInstance();
        with(objXMLHttp){
            try{
                if (url.indexOf("?") > 0){
                    url += "&randnum=" + Math.random();
                }
                else{
                    url += "?randnum=" + Math.random();
                }
                open(method, url, true);   
    setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function (){
                    if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                    {
                        callback(objXMLHttp,funparam,funparam2);
                    }else{
      callback(null,funparam,funparam2);
     }
                }
            }
            catch(e){
                alert(e);
            }
        }
    }
};

/// cookies操作
var Sams_cookies = {

setcookie : function (name, value)
{
  try
  {
   var argv = setcookie.arguments;
   var argc = setcookie.arguments.length;
   var expires = (argc > 2) ? argv[2] : null;
   if(expires!=null)
   {
    var LargeExpDate = new Date ();
    LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
   }
   document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
   return true;
  }
  catch(e)
  {
   return false;
  }
},


getcookie : function (Name)
{
  var search = Name + "="
  if(document.cookie.length > 0) 
  {
   offset = document.cookie.indexOf(search)
   if(offset != -1) 
   {
    offset += search.length
    end = document.cookie.indexOf(";", offset)
    if(end == -1) end = document.cookie.length
    return unescape(document.cookie.substring(offset, end))
   }
   else
   {
    return;
   }
  }
}
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/119266.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号