现在的位置: 首页 > 综合 > 正文

(推荐一个超好的JS函数库)S.Sams Lifexperience ScriptClassLib (引用)

2012年12月24日 ⁄ 综合 ⁄ 共 11279字 ⁄ 字号 评论关闭

http://files.cnblogs.com/HebiZiyu/jsLib.js
出处:http://www.cnblogs.com/meil/archive/2007/04/06/700318.html

//*------------------------------------------------------------
/*
                    S.Sams Lifexperience
* CopyRight (C) 2003-2007 S.Sams Lifexperience ScriptClassLib
* MSNLive: S.Sams#msn.com
* Http://blog.8see.net/
* UPdate by : 2007-01-19 转载传播请保留版权
*-----------------------------------------------------------
*/

/**//*    $ 获取指定对象
   @element    对象名
   可以使用对象名集合,返回值为对象的集合
   如果您使用了 Prototype 类库, 请把该函数注释掉
   Sams_object.Get() 中同样实现该函数的所有功能
*/
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;
   },

   /**//*    设为首页
       @url        要设为首页的地址
   
*/
   SetDefault : 
function ()
   {
       
this.style.behavior='url(#default#homepage)';
       
this.setHomePage(this.GetUrl());
       
return false;
   },

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

   /**//*    加入收藏
       @site        站点名称
       @url        地址
   
*/
   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将本页加入收藏夹");
       }
   },

   /**//*    打开Url指定宽度和高度的窗口 */
   OpenWindows : 
function (url,width,height)
   {
       window.open(url,
'newwin','width='+width+',height='+height);
       
return false;
   },

   /**//*    禁止浏览器的Javascript错误提示 */
   CloseError : 
function(){
       window.onerror 
= function(){return true;};
   },

   /**//*    获取浏览器URL */
   GetUrl : 
function(){
        
return location.href;
    },

    /**//*    获取URL参数 */
    GetUrlParam : 
function(){
        
return location.search;
    },
    
    
/**//*    获取页面来源 */
    GetFrom : 
function(){
        
return document.referrer;
    },

    /**//*    获取指定的URL参数值
        @name        参数名
    
*/
    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<Plist.length; i++)
            {
                
var GetValue = Plist[i].split('=');
                
if (GetValue[0].toUpperCase() == name.toUpperCase())
                {
                    
return GetValue[1];
                    
break;
                }
            }
            
return;
        }
    },

    /**//*    直接将HTML写到新窗口
        @title        标题
        @msg        内容
    
*/
    Popmsg : 
function PopIt(title,msg)
    {
        
var popup = window.open('','popDialog','height=500,width=400,scrollbars=yes');
        popup.document.write(
'<html><title>'+title+'</title><style>body{margin:10px;font:13px Arial;}span{text-line:20px;}</style><body><span style=\'font:14px arial;\'>'+msg + '</span></body></html>');
        popup.document.close();
    }
};

/// 对象操作
var Sams_object = {
    
    
/**//*    创建一个DIV对象
        @ID            要创建的对象ID
        @ClassName    创建对象的Class
        @SetValue    设置该对象值
        @ToDiv        将对象追加到指定的对象,如指定的对象不存在,则追加在Body的后面
        返回        创建后的对象
    
*/
    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);
    },
    
    
/**//*    删除指定DIV对象
        @objid        要删除的对象ID
        返回        Bool 操作结果
    
*/
    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;
        }
    },

    /**//*    获取浏览器对象
        @id        要获取的对象ID
        可以使用对象名集合,返回值为对象的集合
    
*/
    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;
    },
    
    
/**//*    获取对象的值
        @objid        对象ID
    
*/
    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;
        }
    },

    /**//*    设置指定对象的值,实现可以直接赋值或清除操作
        @objid        对象ID
        @inserValue    传入值(可选项 Null:清除该ID的值, 则直接赋值)
    
*/
    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;
        }
    },
    
    
/**//*    拷贝对象值到剪贴板  
        @str        对象值
    
*/
    CopyCode : 
function (str) {
        
var rng = document.body.createTextRange();
        rng.moveToElementText(str);
        rng.scrollIntoView();
        rng.select();
        rng.execCommand(
"Copy");
        rng.collapse(
false);
    },
    
    
/**//*    显示隐藏一个对象 
        @Objid        对象ID
        @isshow        具体操作,指定Obj为False : none或者True : block (可选)
    
*/
    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';
            }
        }
    },
    
    
/**//*    当前对象是否可见
        @objid        对象ID
    
*/
    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 = {
    
/**//*    取左边的指定长度的值  
        @str        要处理的字符集
        @n            长度
    
*/
    Left : 
function (str,n)
    {
        
if(str.length > 0)
        {
            
if(n>str.length) n = str.length;
            
return str.substr(0,n)
        }
        
else
        {
         
return;
        }
    },
    
    
/**//*    取右边的指定长度的值   
        @str        要处理的字符集
        @n            长度
    
*/
    Right : 
function (str,n)
    {
        
if(str.length > 0)
        {
            
if(n>=str.length) return str;
            
return str.substr(str.length-n,n);
        }
        
else
        {
            
return;
        }
    },
    
    
/**//*    Trim:清除两边空格 
        @str        要处理的字符集
    
*/
    Trim : 
function (str) 
    {
        
if (typeof str == 'string'return str.replace(/(^\s*)|(\s*$)/g, '');
    },

    /**//*    LTrim:清除左边的空格 
        @str        要处理的字符集
    
*/
    Ltrim : 
function (str) 
    { 
        
if (typeof str == 'string'return str.replace(/(^\s*)/g, '');
    },

    /**//*    RTrim: 清除右边的空格 
        @str        要处理的字符集
    
*/
    Rtrim : 
function (str) 
    { 
        
if (typeof str == 'string'return str.replace(/(\s*$)/g, '');
    },
    
    
/**//*    清除前后的非字符
        @str        要处理的字符集
    
*/
    strip : 
function(str) {
        
if (typeof str == 'string'return str.replace(/^\s+/'').replace(/(^\s*)|(\s*$)/g, '');
    },

    /**//*    过滤字符里面的HTML标签
        @str        要处理的字符集
    
*/
    stripTags : 
function(str) {
        
if (typeof str == 'string')return str.replace(/<\/?[^>]+>/gi, '').replace(/(^\s*)|(\s*$)/g, '');
    }
};

/// 时间相关操作
var Sams_time = {
    
/**//*    获取当天日期 yyyy-MM-dd */
    GetDateNow : 
function (){
        
var d,y,m,dd;
        d 
= new Date(); 
        y 
= d.getYear();
        m 
= d.getMonth() + 1;
        dd 
= d.getDate();                                                
        
return y+"-"+m+"-"+dd;
    },

    /**//*    获取指定日期后的特定天数的日期值
        @toDate        当前指定的日期
        @N            要添加的日期数
    
*/ 
    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

抱歉!评论已关闭.