常用js

1.常用函数

function(evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }

阻止事件

function(ele, e, call) {
        if (window.addEventListener)
            ele.addEventListener(e, call, false);
        else
            ele.attachEvent("on" + e, call);
    }

添加事件

 

 $("#addClassName").keydown(function(e) {
        if (e.keyCode == 13) {
            e.stopPropagation(); //阻止事件冒泡
            e.preventDefault();
        }     
        }).keyup(function(e) {
            if (e.keyCode == 13) {
                $("#addClassBtn").click();
            }
        });

阻止表单提交

 //验证号码 可以有空格或者-
        function checkPhone(phone) {
            var reg = /^(([0-9]{3}[-| ]?[0-9]{8})|([0-9]{4}[-| +]?[0-9]{7,8})$)/
            return reg.test(phone);
        }

        //  验证号码
        function checkMobile(s) {
            var reg0 = /^13\d{5,9}$/;
            var reg1 = /^153\d{4,8}$/;
            var reg2 = /^159\d{4,8}$/;
            var reg3 = /^0\d{10,11}$/;
            var my = false;
            if (reg0.test(s)) my = true;
            if (reg1.test(s)) my = true;
            if (reg2.test(s)) my = true;
            if (reg3.test(s)) my = true;

            return my;
        }

        //MSN验证
        function checkmsn(s) {
            var reg = /(\S)+[@]{1}(\S)+[.]{1}(\w)/;
            return reg.test(s);
        }

        //QQ号的验证
        function checkqq(s) {
            var reg = /^[1-9]\d{4,11}$/;
            return reg.test(s);
        }

一些验证

var someDate=new Date("Month dd,yyyy")生成date

2.//判断是否为数组

var s="";

alert(Object.prototype.toString.apply(s) === '[object Array]');

//

3.获取html里面的内容 获取body是document.body.innerHTML

<script>
alert(document.documentElement.innerHTML);
</script>

//iframe内容

<iframe id='kefuiframe' src='/aspx/Kefu.aspx' style="border:none; overflow:hidden; height:50px;" scrolling="no" frameborder="0"  allowtransparency="true"></iframe>

<script type="text/javascript">
    $(function() {
    var ifm = document.getElementById("kefuiframe");
    if (ifm.contentDocument)
        ifm.contentDocument.body.style.backgroundColor = "transparent";
    else {
        ifm.contentWindow.document.body.style.backgroundColor = "transparent";
    }
    });
</script>

<iframe src="http://www.fufuok.com/" id="iframepage" name="iframepage" frameBorder=0 scrolling=no width="100%" onLoad="iFrameHeight('iframepage')" ></iframe>
 function iFrameHeight(id) {  
            var ifm= document.getElementById(id);  
            var subWeb = document.frames ? document.frames[id].document : ifm.contentDocument;  
            if(ifm != null && subWeb != null) {
               ifm.height = subWeb.body.scrollHeight;
            }  
        }
//下面方法是设置动态显示滚动条
            var obj = $('#iframeTotal');
            obj.hover(function(){
                this.scrolling = 'auto';
                this.contentWindow.document.body.scroll = 'yes';
            },function(){
                  this.scrolling = 'no';
                this.contentWindow.document.body.scroll = 'no';
            });
//定适应
 function autoIframe(ifmobj,minHeight)
        {
           if (document.all(ifmobj.id).readyState == "complete") {
               try
               { var sHeight = window.frames[ifmobj.id].document.body.scrollHeight;
                 document.all(ifmobj.id).style.height = sHeight+10;   
               }
               catch(xx)
               {
                    return ;
               }
           }
        }

4.设置元素在中间

                                  var div = $("<div style='position:absolute; background-color:yellow;padding:20px;'>页面正在跳转,请稍等...</div>");
                                  var height = document.documentElement.clientHeight-115;
                                  var width = document.documentElement.clientWidth - 115;
                                  div.css("top", height / 2).css("left", width / 2);
                                  $("body").append(div);

5.打印

function func_print(){
        var win = window.open(document.getElementById("iframePrint").src);
        win.print();
    }

posted on 2010-06-30 16:55  jianshaohui  阅读(211)  评论(1编辑  收藏  举报

导航