一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

一些常用的JS功能函數(shù)(2009-06-04更新)

復制代碼 代碼如下:
//獲取對象
function getObject(objectId,top)
{
    doc = top?window.top.document:document;
    if(typeof(objectId)!="object" && typeof(objectId)!="function")
    {
        if(doc.getElementById && doc.getElementById(objectId))
         {
         // W3C DOM
         return doc.getElementById(objectId);
         }
        else if(doc.getElementsByName(objectId))
        {
            return doc.getElementsByName(objectId)[0];
        }
         else if (doc.all && doc.all(objectId))
         {
         // MSIE 4 DOM
         return doc.all(objectId);
         }
         else if (doc.layers && doc.layers[objectId])
         {
         // NN 4 DOM.. note: this won't find nested layers
         return doc.layers[objectId];
         }
         else
         {
         return false;
         }
    }else
        return objectId;
}
//獲取相對路徑
function getRelativePath()
{
    var url = location.href;//當前url
    var urlcs = String(location.search);
    url = url.replace(urlcs,"");
    var path = url.length - url.replace(////g,"").length - 3;    //層次為url包含/的長度-沒有包含/的長度再減去項目頭/的個數(shù)

    var str = "";
    for(var i = 0; i < path; i++)
    {
     str += "../";//組合成一個相對路徑的字符串返回
    }
    return str;
}
//加載其他JS文件或CSS文件
function loadjscssfile(filename,filetype,chkonce)
{
    filetype = !filetype?"js":filetype;
    var had = false;
    if(filetype=="js")
    {
        if(chkonce)
        {
            var allScripts = document.getElementsByTagName("script");
            for(var i=0;i<allScripts.length;i++)
            {
                try{
                    if(allScripts[i].src.indexOf(filename)>-1)
                    {
                        had = true;
                        break;
                    }
                }catch(e){}
            }    
        }
        if(!had)
        {
            document.write("<script src=""+filename+"" src=""+filename+"" type='text/Javascript'></script>");
        }
    }else
    {
        if(chkonce)
        {
            var allCss = document.getElementsByTagName("link");
            if(allCss.length)
            {
                for(var i=0;i<allCss.length;i++)
                {
                    try{
                        if(allCss[i].href.indexOf(filename)>-1)
                        {
                            had = true;
                            break;
                        }
                    }catch(e){}
                }
            }
        }
        if(!had)
        {
            document.write("<link type='text/css' rel='stylesheet' href=""+filename+"" href=""+filename+"" />");
        }
    }
}
//定義根目錄路徑
var ROOT_PATH = getRelativePath();
var JS_PATH = ROOT_PATH+'js/';
var AJAX_PATH = ROOT_PATH+'ajax/';
var CSS_PATH = ROOT_PATH+'css/';
var IMAGES_PATH = ROOT_PATH+'images/';
var EDITOR_PATH = ROOT_PATH+'uploadeditor/';
var PUB_PATH = ROOT_PATH+'uploadfile/';

//加載公共變量的JS
loadjscssfile(JS_PATH+"globalPara.js");

//設置下拉表中某一項被選中
function setSelOption(objId,vlu)
{
    objId = getObject(objId);
    for(var i=0;i<objId.options.length;i++)
    {
        if(objId.options[i].value==vlu)
        {
            objId.options[i].setAttribute("selected","selected");
            break;
        }
    }
}
//根據(jù)下拉表中的option文本設置某一項被選中
function setTxtOption(objId,txt)
{
    objId = getObject(objId);        
    for(var i=0;i<objId.options.length;i++)
    {
        if(objId.options[i].innerHTML==txt)
        {
            objId.options[i].setAttribute("selected","selected");
            break;
        }
    }    
}

//設置單選按鈕組中某一項被選中
function setSelRadio(objName,vlu)
{
    objName = document.getElementsByName(objName);
    for(var i=0;i<objName.length;i++)
    {
        if(objName[i].value==vlu)
        {
            objName[i].setAttribute("checked","checked");
            break;
        }
    }
}

//根據(jù)ID設置復選框中某些項被選中
//vlu 的格式為 : 1,2,3
function setSelCheckbox(prefix,vlu)
{
    var _arr = vlu.split(",");
    if(_arr!="")
    {
        for(var i=0; i<_arr.length; i++)
        {
            getObject(prefix+_arr[i]).checked = true;
        }
    }
}

//快捷輸入,fromObj:來源對象,toObjId:目標對象的ID,txt為true時取toObjId的innerHTML值
function fastInput(fromObj,toObjId,txt)
{
    if(fromObj.value=='' || fromObj.value==0) return false;
    txt = !txt ? false : txt;
    var toObj = getObject(toObjId);
    if(txt)
    {
        if(typeof (toObj.value) == 'undefined')
             toObj.innerHTML = toTxt(fromObj.options[fromObj.selectedIndex].innerHTML);
        else
            toObj.value = toTxt(fromObj.options[fromObj.selectedIndex].innerHTML);
    }else
    {
        if(typeof (toObj.value) == 'undefined')
             toObj.innerHTML = fromObj.value;
        else
            toObj.value = fromObj.value;        
    }
}

/*
IE6,IE7上傳圖片前預覽圖片
IE6下還可以同時檢測圖片的大小
size 單位為KB
<style type="text/css"><!--

.newPreview
{
    width:400px; height:300px;
    FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
.nodis{display:none;}
--></style><style type="text/css" bogus="1">
.newPreview
{
    width:400px; height:300px;
    FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
.nodis{display:none;}</style>
<input type="file" name="litpic" onchange="PreviewImg(this,'newPreview','ndsPreview',400)" />
<div id="newPreview" class="newPreview"></div>
<img id="ndsPreview" class="nodis" />
*/
function PreviewImg(imgFile,newPreview,ndsPreview,size)
{
    newPreview = getObject(newPreview);
    if(!imgFile || !imgFile.value || !newPreview){return};
    var patn = //.jpg$|/.jpeg$|/.gif$|/.png$|/.bmp$/i;
    if(patn.test(imgFile.value))
    {
        try{newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;}catch(e){}
        if(navigator.appVersion.indexOf("MSIE 6.0",0)>-1)
        {
            size=!size?0:size;
            ndsPreview = getObject(ndsPreview);
            if(ndsPreview)
            {
                try{ndsPreview.attachEvent("onreadystatechange", function(){checkImgSize(ndsPreview,size)})}catch(e){}
                ndsPreview.src = imgFile.value;
            }
        }
    }
    else
    {
        alert("您選擇的不是圖像文件,請重新選擇.");
    }
}
function PreviewImgNow(imgDiv,imgFile)
{
    try{getObject(imgDiv).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile}catch(e){}    
}
//上傳之前檢測圖片的大小
//條件是在file改變時要觸發(fā)函數(shù)將縮略圖顯示在img上
//size 單位為KB
function checkImgSize(img,size)
{
    img = getObject(img);
    if(img.readyState == "complete")
    {
        var limit = size * 1024;
        if(img.fileSize > limit)
        {
            alert("出錯!您上傳的圖片大小為"+(parseInt(img.fileSize/1024))+"KB,超過了"+size+"KB的限制,請重新上傳");
            return false
        }else
            return true;
    }
    return true;
}
//檢測客戶端環(huán)境
function ClientMentInfo()
{
    var me = this;    
    var appVer = navigator.userAgent;

    this.GetBrowserName = function (){
        if(appVer.indexOf("MSIE")>0) return "IE";
        else if(appVer.indexOf("Firefox")>0) return "Firefox";
        else if(appVer.indexOf("Chrome")>0) return "Chrome";
        else if(appVer.indexOf("Safari")>0) return "Safari";
        else if(appVer.indexOf("Camino")>0) return "Camino";
        else if(appVer.indexOf("Konqueror")>0) return "Konqueror";
        else return "其它";
    }

    this.GetOSInfo = function (){
        var _pf = navigator.platform;
        if(_pf == "Win32" || _pf == "Windows")
        {

            if(appVer.indexOf("Windows NT 6.0") > -1 || appVer.indexOf("Windows Vista") > -1)
            {
                return 'Windows Vista';    
            }else if(appVer.indexOf("Windows NT 6.1") > -1 || appVer.indexOf("Windows 7") > -1)
            {
                return 'Windows 7';    
            }else
            {
                try{
                    var _winName = Array('2000','XP','2003');
                    var _ntNum = appVer.match(/Windows NT 5./d/i).toString();
                    return 'Windows ' + _winName[_ntNum.replace(/Windows NT 5.(/d)/i,"$1")];
                }catch(e){return 'Windows';}
            }
        }else if(_pf == "Mac68K" || _pf == "MacPPC" || _pf == "Macintosh")
        {
            return "Mac";            
        }else if(_pf == "X11")
        {
            return "Unix";    
        }else if(String(_pf).indexOf("Linux") > -1)
        {
            return "Linux";    
        }else
        {
            return "Unknow";    
        }        
    }

    this.OS = me.GetOSInfo();        //操作系統(tǒng)類型
    this.IeVer = null;
    this.Bs_lang = (navigator.appName == 'NETscape'?navigator.language:navigator.browserLanguage);//瀏覽器語言版本
    this.Bs_Name = me.GetBrowserName();        //瀏覽器名稱

    //瀏覽器版本
    if(this.Bs_Name=='IE')
    {
        var _msie = appVer.match(/MSIE /d./i).toString();
        this.Bs_Version = this.IeVer = _msie.replace(/MSIE (/d)./i,"$1");
    }else
    {
        this.Bs_Version = appVer;
    }
    this.Ie6 = this.IeVer==6 ? true: false;
    this.Ie7 = this.IeVer==7 ? true: false;
    this.Ie8 = this.IeVer==8 ? true: false;
}
//客戶端信息
var CMInfo = new ClientMentInfo();
//IE6 下緩存背景圖片
if(CMInfo.Ie6)
{
    document.execCommand("BackgroundImageCache", false, true);
}
//設置復選框全選或全不選
function setAllCheckbox(formName,objName,num)
{
    if(formName)
        _arr = getObject(formName).elements[objName];
    else
        _arr = typeof(objName)=="object"?objName:document.all(objName);
    if(_arr)
    {
        if(num)
        {
            if (!_arr.length ) // 只有一個復選框,則length = undefined
                _arr.checked = true;
            else
            {
                for(var i=0; i<_arr.length; i++)
                {
                    _arr[i].checked = true;
                }
            }
        }else
        {
            if (!_arr.length ) // 只有一個復選框,則length = undefined
                _arr.checked = false;
            else
            {
                for(var i=0; i<_arr.length; i++)
                {
                    _arr[i].checked = false;
                }
            }
        }
    }
}

//使當前頁面跳到指定的頁數(shù)頁面
function goPage(pageNum,pageStr)
{
    window.location.href = "?np="+pageNum+pageStr;
}
//分別去字符串前后,左邊,右邊空格
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,"")}

注意globalPara.js本作者常用到的一些每個站自己的常用變量和常量的文件,如果你不需要這樣的文件,可把
//加載公共變量的JS
loadjscssfile(JS_PATH+"globalPara.js");

這兩句刪掉,不然會報錯。

(2009-06-04更新)
將 ClientMentInfo類改成了兼容IE6,IE7,IE8,Vista,Windows 7和Firefox

JavaScript技術(shù)一些常用的JS功能函數(shù)(2009-06-04更新),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产综合欧美日韩视频一区 | 天堂成人精品视频在线观 | 国产麻豆福利a v在线播放 | 国产欧美一区二区三区在线看 | 婷婷色网| 精品一二三四区 | 久久久久中文字幕 | 欧美无毛逼 | 热久久伊人| 国产在线91观看免费观看 | 青青草久热精品视频在线观看 | 久久久久久穴 | 国产成人精品久久综合 | 91中文字幕在线视频 | 国产精品久久久久久五月尺 | 国产精久久一区二区三区 | 久热精品视频在线观看99小说 | 色视频哟女在线观看 | 毛片特黄 | 国产成人激烈叫床声视频对白 | 永久黄网站色视频免费 | 日韩欧美国产三级 | 天天做天天爱夜夜爽 | 黄色在线观看国产 | 黄色网视频| 91精品久久久 | 天天色天天做 | 国产高清国内精品福利99久久 | 2021久久精品99精品久久 | 免费成人在线观看 | 天堂色| 欧美精品v国产精品v | 加勒比一区在线 | 五月开心激情 | 日本高清www色 | 国产精品一区二区久久 | 一区二区手机视频 | 91精品久久久久 | 成人亚洲视频 | 国产精品视频一区二区亚瑟 | 日本高清www视频在线观看 |