一些javascript函数

 

判断字符串是不是数字

   function isInteger(strInteger)
{
 strInteger=trimStr(strInteger);
    return strInteger.match(/^/d+$/);
}

 

//判断是否为空。

function trimStr(str)
{
    if( str == undefined || str == null ) return '';
   
    return str.replace(/(^/s*)|(/s*$)/g,'');
}

function isEmptyStr(obj)
{
    return ''==trimStr(obj);
}

 

字符串编码

encodeURI(searchText)

 

//padleft函数

function padLeft(str,iLength,pad){
              var sMsg=str.toString();
               while(sMsg.length<iLength){sMsg=pad+sMsg;}
                 return sMsg;
               }

取得距离顶部的高度//  任何情况下都可以实现   如果要给层赋值的话要加上“px”

if (document.documentElement && document.documentElement.scrollTop) {    
     diffY= document.documentElement.scrollTop;     
     }
     else if (document.body)
      {
       diffY = document.body.scrollTop;
      }

 

$("floatdiv").style.top    top 是所有的浏览器认的属性,  而pixedtop则不是,只有IE才认。

 

 

//页面滚动条长度
function getPageScroll(){
  var yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }

  arrayPageScroll = new Array('',yScroll)
  return arrayPageScroll;
}

//页面大小
function getPageSize(){
  var xScroll, yScroll; 
  if (window.innerHeight && window.scrollMaxY) {
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  } 
 
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }

  if(xScroll < windowWidth){ 
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  return arrayPageSize;
}

//页面滚动到顶端
function doScrollPage(scrolltop){
  if (self.pageYOffset) {
    self.pageYOffset=scrolltop;
  } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
    document.documentElement.scrollTop=scrolltop;
  } else if (document.body) {// all other Explorers
    document.body.scrollTop=scrolltop;
  }
}

//显示载入状态
function showLoading(_id,_content){
     if($(_id)==null){var loadDivObj = document.createElement("div");loadDivObj.className = "loadingdiv";loadDivObj.id = _id;loadDivObj.EventInit = false;document.body.appendChild(loadDivObj);}else{var loadDivObj = $(_id);}if (_content){loadDivObj.innerHTML = "<div  class=/"loadingtext/" style=/"padding-top:5px;/"><img src=/"http://f.xgou.com/cms2008/images/loading.gif/"></div><div class=/"loadingtext/">"+_content+"</div>";}else{loadDivObj.innerHTML = "<div  class=/"loadingtext/" style=/"padding-top:5px;/"><img src=/"http://f.xgou.com/cms2008/images/loading.gif/"></div><div class=/"loadingtext/">正在载入数据,请稍后...</div>";}
}

//页面被灰色覆盖
function showCover(){
    if($("divPageCover")==null){
      var coverDivObj = document.createElement("div");coverDivObj.className = "pagecover";coverDivObj.id = "divPageCover";coverDivObj.EventInit = false;document.body.appendChild(coverDivObj);
    }
    var psize = getPageSize();
   
    var objst = $("divPageCover").style;
    objst.display="none";
    objst.width = psize[2].toString()+"px";
    objst.height = getPageSize()[1].toString()+"px";
    objst.filter = "alpha(opacity=70)";objst.opacity = 70/100;objst.MozOpacity = 70/100;
    //Element.show("divPageCover");
    Effect.Appear("divPageCover",{duration:0.3,to:0.5});
}
//显示覆盖层时隐藏select表单控件
function pageClear(){
  var allselect = document.getElementsByTagName("select");
  for (var i=0; i<allselect.length; i++) allselect[i].style.visibility = "hidden";
}
//取消层覆盖,恢复显示select表单控件
function pageUnClear(){
    if($("divPageCover") && $("divPageCover").style.display!="none"){
       Effect.Fade("divPageCover",{duration:0.3,onEnd:function(){Element.hide("divPageCover")}});
    }
    var objselect = document.getElementsByTagName("select");
    for (var i=0; i<objselect.length; i++) objselect[i].style.visibility = "visible";
}
//提示框
function showAlert(words,clickobj){
 var _html ="";
 if(words.length>0){
     _html = words+"<br/><input type=/"button/" class=/"dialogbtn/" value=/"确定/" οnclick=/"hideDialog()/" />";
   }
 if(clickobj){
   showDialog(_html,280,150,0,24,clickobj)
 }
 else{
  showDialog(_html,280,150,0,-20)
 }
}
//对话框
function showConfirm(words,okfun,clickobj){
 var _html = words+"<br/><input type=/"button/" class=/"dialogbtn/" value=/"是/" οnclick=/""+okfun+"/"><input type=/"button/" class=/"dialogbtn/" value=/"否/" οnclick=/"hideDialog()/" />"
 if(clickobj){
   showDialog(_html,280,150,0,24,clickobj)
 }
 else{
  showDialog(_html,280,150,0,-20)
 }
}
//对话框2
function showConfirm2(words,okfun,cancelfun,yesbtn,nobtn,clickobj){
 var _html = words+"<br/><input type=/"button/" class=/"dialogbtn/" style=/"width:auto;/" value=/""+yesbtn+"/" οnclick=/""+okfun+"/"><input type=/"button/" class=/"dialogbtn/" style=/"width:auto;/" value=/""+nobtn+"/"  οnclick=/""+cancelfun+";hideDialog();/" />"
 if(clickobj){
   showDialog(_html,300,150,0,24,clickobj)
 }
 else{
  showDialog(_html,300,150,0,-20)
 }
}
//显示对话框
function showDialog(_html,wt,ht,ptx,pty,clickobj,dialogid){
   var dlgid = dialogid==null ? "dialogDivOut" : dialogid;
    if($(dlgid)==null){
      var dialogDivOut = document.createElement("div");dialogDivOut.className="pagedialogout";dialogDivOut.id = dlgid;dialogDivOut.EventInit = false;document.body.appendChild(dialogDivOut);
       var dialogDivObj = document.createElement("div");dialogDivObj.className = "pagedialog";dialogDivObj.id = dlgid+'_1';dialogDivObj.EventInit = false;dialogDivOut.appendChild(dialogDivObj);
       var dialogDivBg = document.createElement("div");dialogDivBg.className = "pagedialogbg";dialogDivBg.id = dlgid+'_2';dialogDivBg.EventInit = false;dialogDivOut.appendChild(dialogDivBg);
    }
    var objstout = $(dlgid).style;
    var objst = $(dlgid+'_1').style;
    var objstbg = $(dlgid+'_2').style;
   
    var psize = getPageSize();
    var ssize = getPageScroll();
   
    objstout.display="none";
    objstout.height=(ht+8).toString()+"px";
    objstout.width = (wt+8).toString()+"px";
   
    objst.display="none";
    objst.height=ht.toString()+"px";
    objst.width = wt.toString()+"px";
  
    objstbg.display="none";
    objstbg.width = wt.toString()+"px";
    objstbg.height = ht.toString()+"px";
    objstbg.filter = "alpha(opacity=20)";objst.opacity = 20/100;objst.MozOpacity = 20/100;
   
      objst.top="0px";
      objst.left="0px";
      objstbg.top = "6px";
      objstbg.left="6px";
   
    if(clickobj){
      Position.clone(clickobj, dlgid,{offsetTop:pty,offsetLeft:ptx,setWidth:false,setHeight:false});
    }
    else{
     objstout.top = ((parseInt(psize[3].toString())-ht)/2+parseInt(ssize[1].toString())+pty).toString() + "px";
     objstout.left = ((parseInt(psize[2].toString())-wt)/2+ptx).toString() + "px";   
    }
    if(_html=="")_html="<img src=/""+loadImg.src+"/" align=/"absmiddle/"/>&nbsp;&nbsp;正在载入数据...";
    $(dlgid+'_1').innerHTML ="<div class=/"dialogtop/"><div οnclick=/"hideDialog('"+dlgid+"')/" title=/"关闭/"></div></div><table class=/"dialogContent/" cellpadding=/"0/" cellspacing=/"0/" style=/"height:"+(ht-23).toString()+"px/"><tr><td id=/""+dlgid+"_content/">"+_html+"</td></tr></table>";
    Effect.Appear(dlgid,{duration:0.3,to:1.0});
    Effect.Appear(dlgid+'_1',{duration:0.3,to:1.0});
    Effect.Appear(dlgid+'_2',{duration:0.5,to:0.3});
   
    new Draggable(dlgid,{scroll:window,handle:'dialogtop',revert:true,endeffect:function(){}});
    pageClear();
}

//写对话框文字
function writeDialog(_html,showbtn,dialogid){
 var dlgid = dialogid==null ? "dialogDivOut" : dialogid;
 if(showbtn){
  _html+="<br/><input type=/"button/" class=/"dialogbtn/" value=/"确定/" οnclick=/"hideDialog('"+dlgid+"')/" />";
 }
 $(dlgid+"_content").innerHTML=_html;
}
//隐藏对话框
function hideDialog(dialogid){
  var dlgid = dialogid==null ? "dialogDivOut" : dialogid;
  if($(dlgid)){Effect.Fade(dlgid,{duration:0.3});$(dlgid).isshow=false;}
  if($(dlgid+'_2'))Effect.Fade(dlgid+'_2',{duration:0.3})
   if($(dlgid+'_1'))Effect.Fade(dlgid+'_1',{duration:0.3})
   pageUnClear();
}

//拷贝层
function cloneDiv(clickobj,divid,pointx,pointy)
{
    Element.hide(divid);
    Position.clone(clickobj, divid,{offsetTop:pointy,offsetLeft:pointx,setWidth:false,setHeight:false});
    Element.show(divid);
}

//将汉字长度转换为2个字节
function lenB(str){
  return str.replace(/[^/x00-/xff]/g,"**").length
}

//去左右空格
String.prototype.trim = function()
{
 return this.replace(/(^/s*)|(/s*$)/g, "");
}

//取字符串长度
String.prototype.ByteCount = function()
{
 txt = this.replace(/(<.*?>)/ig,"");
 txt = txt.replace(/([/u0391-/uFFE5])/ig, "11");
 var count = txt.length;
 return count;
}

//格式转换
 function TextToHtml(chr)
 {
    if (chr == null) return "";
    chr = strReplace("<", "&lt;",chr);
    chr = strReplace(">", "&gt;",chr);
    chr = strReplace("/n", "<BR>",chr);
    chr = strReplace(" ", "&nbsp;",chr);
    chr = strReplace("'", "’",chr);
    chr = strReplace("/"", "”",chr);
    return chr;
 }
 
  function HtmlToText(chr)
 {
    if (chr == null) return "";
    chr = strReplace("<BR>","/n", chr);
    chr = strReplace("<br>","/n", chr);
    chr = strReplace("&lt;","<",chr);
    chr = strReplace("&gt;",">", chr);
    chr = strReplace("&nbsp;"," ",chr);
    chr = strReplace("’", "'",chr);
    chr = strReplace("”", "/"",chr);
    return chr;
 }
 
 function JsFormat(chr){
   if (chr == null) return "";
   chr = strReplace("'","''",chr);
   return chr
 }
 
 function JsReFormat(chr){
   if (chr == null) return "";
   chr = strReplace("''","'",chr);
   return chr
 }
       
//字符替换
function strReplace(search, replace, str){
 var regex = new RegExp(search, "g");
 return str.replace(regex, replace);
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
   if(result.childNodes.length < 1)
//    return "n/a";
                return "";
            return result.firstChild.nodeValue;      
        }
  
    } else {
//        return "n/a";
          return "";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值