zoukankan      html  css  js  c++  java
  • js常用的方法

    目录

      在Jquery里面,我们可以看到两种写法:

    $(function(){}) 和$(document).ready(function(){})
    
    

      这两个方法的效果都是一样的,都是在dom文档树加载完之后执行一个函数(注意,这里面的文档树加载完不代表全部文件加载完)。

      而window.onload是在dom文档树加载完和所有文件加载完之后执行一个函数。也就是说$(document).ready要比window.onload先执行。

      简单用法如下

       window.onload = function () {
                alert('onload');
    
            };
    
    document.ready(function () {
        alert('ready');
    
            });
    
    document.ready = function (callback) {
                ///兼容FF,Google
                if (document.addEventListener) {
                    document.addEventListener('DOMContentLoaded', function () {
                        document.removeEventListener('DOMContentLoaded', arguments.callee, false);
                        callback();
                    }, false)
                }
                 //兼容IE
                else if (document.attachEvent) {
                    document.attachEvent('onreadystatechange', function () {
                          if (document.readyState == "complete") {
                                    document.detachEvent("onreadystatechange", arguments.callee);
                                    callback();
                           }
                    })
                }
                else if (document.lastChild == document.body) {
                    callback();
                }
            }
    

    字符串相关的

    判断以什么开头

    方法1:

    substr() 方法

    if("123".substr(0, 2) == "12"){
        console.log(true);
    }

    方法2:

    substring() 方法

    if("123".substring(0, 2) == "12"){
        console.log(true);
    }

    方法3:

    slice()方法

    if("123".slice(0,2) == "12"){
        console.log(true);
    }

    方法4:

    indexOf() 方法

    if("123".indexOf("12") == 0) {
        console.log(true);
    }

    方法6:

    正则

    复制代码
    if("123".search("12") != -1) {
        console.log(true);
    }
    

    if(new RegExp("^12.*$").test(12)) {
    console.log(
    true);
    }

    if("12".match(new RegExp("^12.*$"))) {
    console.log(
    true);
    }

    复制代码

    时间相关的

    计算当前时间

     Date.prototype.Format = function (fmt) {
                                var o = {
                                    "M+": this.getMonth() + 1,                 //月份
                                    "d+": this.getDate(),                    //日
                                    "h+": this.getHours(),                   //小时
                                    "m+": this.getMinutes(),                 //分
                                    "s+": this.getSeconds(),                 //秒
                                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                                    "S": this.getMilliseconds()             //毫秒
                                };
                                if (/(y+)/.test(fmt)) {
                                    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                                }
                                for (var k in o) {
                                    if (new RegExp("(" + k + ")").test(fmt)) {
                                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                                    }
                                }
                                return fmt;
                            };//计算日期
    
                            var myDate = (new Date()).Format("yyyy-M-d h:m");
    
  • 相关阅读:
    EntityFramework 启用迁移 EnableMigrations 报异常 "No context type was found in the assembly"
    JAVA 访问FTP服务器示例(2)
    NuGet Package Manager 更新错误解决办法
    JAVA 访问FTP服务器示例(1)
    RemoteAttribute 的使用问题
    诡异的 javascript 变量
    javascript apply用法
    Babun 中文乱码
    GSM呼叫过程
    转站博客园
  • 原文地址:https://www.cnblogs.com/AbnerLc/p/11167235.html
Copyright © 2011-2022 走看看