JS 一些技巧及工具方法【转】

1           日期验证方法

function validateDateType(paramDate){

     isDateType=false;

      vbParam=paramDate

          execScript("isDateType = IsDate(vbParam)", "VBScript");

          alert(isDateType);

      return isDateType;

  }

对于js来说验证数据类型是很麻烦的事情,

要判断很多规则,平年,闰年 大月 小月

再加上时间,可能会啰里啰唆写一大堆的代码出来,

就是用正则也是

如果用VBScript去验证日期类型那就简单的多了,直接使用判断日期数据类型函数就可以达到目的。

对于IE 浏览器来说可以说是以个比较好用的方法。

其他浏览器没有试过。

 

2           获取控件的绝对位置

function getAbsolutePosition(obj){

     var x=obj.offsetLeft;

        var y=obj.offsetTop;

        while(obj=obj.offsetParent){

            x+=obj.offsetLeft;

               y+=obj.offsetTop

        }

        return {x:x,y:y};

}

调用方法

var x=getAbsoutePosition(document.getElementById(id)).x;

Var y= getAbsoutePosition(document.getElementById(id)).y;

 

3           去除字符串的左右空格

String.prototype.trim=function(){

    return this.replace(/^\s+|\s+$/g,””);

}

使用方法

var userNo=document.form1.userNo.value;

if(userNo.trim().length==0){

    alert(“用户号码为空”);

}

 

4           去除左空格

String.prototype.ltrim=function(){

    return this.replace(/^\s+/,””);

}

使用方法

3

 

5           去除右空格

String.prototype.rtrim=function(){

   Return this.replace(/\s+$/,””);

}

使用方法

3

 

6           字符串字节长度

String.prototype.lengthB=function(){

    var len=0;

       for(var i=0;i<this.length;i++){

           len+=new RegExp(/[^x00-xff]/).test(this.charAt(i))?2:1;

       }

       return len;

}

使用方法

3

 

7           自适应框架

function fit(obj){

        var doc=obj.document;

           var h1=0;

           var h2=0;

 

        if(document.body){

          h1= parseInt(doc.body.scrollHeight);

        }

              if(document.documentElement){

                  h2=parseInt(doc.documentElement.scrollHeight);

              }

              var h=h1>h2?h1:h2;

              if(document.all){

                  obj.style.height=h;

                     while(obj.parentNode.tagName.toLowerCase()!="body"){

                     obj=obj.parentNode;

                      if obj.style.height){

                          obj.style.height=h;

                            }

                     }

              }else{

                  obj.style.pixelHeight=h+"px";

                     while(obj.parentNode.tagName.toLowerCase()!="body"){

                         obj=obj.parentNode;

                            if(obj.style.pixelHeight){

                                   obj.style.pixelHeight=h+"px";

                            }

                     }

              }

     }

使用方法

<iframe οnlοad=”fit(this)” src=”…”></iframe>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值