相思资源网 Design By www.200059.com

数组方法集

Angela.array = {  //# 数组方法
  // index, 返回位置! 不存在则返回 -1;
  index: function (t, arr) { //# 返回当前值所在数组的位置
    if (arr.indexOf) {
      return arr.indexOf(t);
    }
    for (var i = arr.length ; i--;) {
      if (arr[i] === t) {
        return i * 1;
      }
    };
    return -1;
  }
  //返回对象 的 键值! 返回值 类型为数组。
  , getKey: function (data) { //# 返回对象所有的键值
    var arr = []
      , k
    ;
    for (k in data) {
      arr.push(k);
    };
    return arr;
  }
  //从数组中 随机取出 一个值
  , random: function (arrays) { //# 从数组中 随机取出 一个值
    arrays = arrays || [];
    var len = arrays.length
      , index = Tydic.math.randInt(0, len - 1)
    ;
    return arrays[index] || '';
  }
  // 一维 数组去重
  , unique: function (array) { //#一维数组去重
    array = array || [];
    for (var i = 0, len = array.length; i < len; i++) {
      for (var j = i + 1; j < array.length; j++) {
        if (array[i] === array[j]) {
          array.splice(j, 1);
          j--;
        }
      }
    }
    return array;
  }
  // max , 数组中最大的项
  , max: function (array) {//#求数组中最大的项
    return Math.max.apply(null, array);
  }
  // min , 数组中最小的项
  , min: function (array) { //#求数组中最小的项
    return Math.min.apply(null, array);
  }
  // remove , 移除
  , remove: function (array, value) { //#移除数组中某值
    var length = array.length;
    while (length--) {
      if (value === array[length]) {
        array.splice(length, 1);
      }
    }
    return array;
  }
  //清空数组
  , empty: function (array) { //# 清空数组
    (array || []).length = 0;
    return array;
  }
  // removeAt ,删除指定位置的 值
  //@index , 索引. 不传递 index ,会删除第一个
  , removeAt: function (array, index) { //#删除数组中 指定位置的值
    array.splice(index, 1);
    return array;
  }
  //打乱数组排序
  , shuffle: function (arr) { //#打乱数组排序
    var array = (arr || []).concat()
      , length = array.length
      , i = length //遍历
      , tmp = null // 临时
      , rand = Tydic.math.randInt //位置
      , pos = 0
    ;
    while (i--) {
      pos = rand(0, length);
      //交换随机位置
      tmp = array[pos];
      array[pos] = array[i];
      array[i] = tmp;
    }
    return array;
  }
};

cookie方法集

Angela.cookie = { //# Cookie
  // 浏览器是够支持 cookie
  enable: !!navigator.cookieEnabled
  //读取COOKIE
  , get: function (name) { //#读取 cookie
    var reg = new RegExp("(^| )" + name + "(")
      , val = document.cookie.match(reg)
    ;
    return val "") : '';
  }
  //写入COOKIES
  , set: function (name, value, expires, path, domain, secure) { //# 写入 cookie
    var exp = new Date()
      , expires = arguments[2] || null
      , path = arguments[3] || "/"
      , domain = arguments[4] || null
      , secure = arguments[5] || false
    ;
    expires "";
    document.cookie = name + '=' + escape(value) + (expires "/";
      document.cookie = name + '=;expires=' + exp.toGMTString() + (path "htmlcode">
Angela.url = { //#URL
  //参数:变量名,url为空则表从当前页面的url中取
  getQuery: function (name, url) {
    var u = arguments[1] || window.location.search
      , reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
      , r = u.substr(u.indexOf("") + 1).match(reg)
    ;
    return r != null "";
  }
  , getHash: function (name, url) { //# 获取 hash值
    var u = arguments[1] || location.hash;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = u.substr(u.indexOf("#") + 1).match(reg);
    if (r != null) {
      return r[2];
    }
    return "";
  }
  , parse: function (url) { //# 解析URL
    var a = document.createElement('a');
    url = url || document.location.href;
    a.href = url;
    return {
      source: url
      , protocol: a.protocol.replace(':', '')
      , host: a.hostname
      , port: a.port
      , query: a.search
      , file: (a.pathname.match(/([^\/"htmlcode">
Angela.regExp = { //# 字符串匹配
  //是否为 数字!整数,浮点数
  isNum: function (num) { //# 是否为数组
    return !isNaN(num);
  }
  , isEmail: function (mail) {//# 是否为 邮箱
    return /^([a-z0-9]+[_\-\.]"htmlcode">
Angela.string = { //# 字符串
  codeHtml: function (content) { //# 转义 HTML 字符
    return this.replace(content, {
      '&': "&amp;"
      , '"': "&quot;"
      , "'": '&#39;'
      , '<': "&lt;"
      , '>': "&gt;"
      , ' ': "&nbsp;"
      , '\t': "&#09;"
      , '(': "&#40;"
      , ')': "&#41;"
      , '*': "&#42;"
      , '+': "&#43;"
      , ',': "&#44;"
      , '-': "&#45;"
      , '.': "&#46;"
      , '/': "&#47;"
      , '"&#63;"
      , '\\': "&#92;"
      , '\n': "<br>"
    });
  }
  //重复字符串
  , repeat: function (word, length, end) { //# 重复字符串
    end = end || ''; //加在末位
    length = ~~length;
    return new Array(length * 1 + 1).join(word) + '' + end;
  }
  //增加前缀
  , addPre: function (pre, word, size) { //# 补齐。如给数字前 加 0
    pre = pre || '0';
    size = parseInt(size) || 0;
    word = String(word || '');
    var length = Math.max(0, size - word.length);
    return this.repeat(pre, length, word);
  }
  //去除两边空格
  , trim: function (text) { //# 去除两边空格
    return (text || '').replace(/^\s+|\s$/, '');
  }
  //字符串替换
  , replace: function (str, re) { //# 字符串替换
    str = str || '';
    for (var key in re) {
      replace(key, re[key]);
    };
    function replace(a, b) {
      var arr = str.split(a);
      str = arr.join(b);
    };
    return str;
  }
  , xss: function (str, type) { //# XSS 转义
    //空过滤
    if (!str) {
      return str === 0 "0" : "";
    }
    switch (type) {
      case "html": //过滤html字符串中的XSS
        return str.replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g, function (r) {
          return "&#" + r.charCodeAt(0) + ";"
        }).replace(/ /g, "&nbsp;").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />");
        break;
      case "htmlEp": //过滤DOM节点属性中的XSS
        return str.replace(/[&'"<>\/\\\-\x00-\x1f\x80-\xff]/g, function (r) {
          return "&#" + r.charCodeAt(0) + ";"
        });
        break;
      case "url": //过滤url
        return escape(str).replace(/\+/g, "%2B");
        break;
      case "miniUrl":
        return str.replace(/%/g, "%25");
        break;
      case "script":
        return str.replace(/[\\"']/g, function (r) {
          return "\\" + r;
        }).replace(/%/g, "\\x25").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x01/g, "\\x01");
        break;
      case "reg":
        return str.replace(/[\\\^\$\*\+\"\\" + a;
        });
        break;
      default:
        return escape(str).replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g, function (r) {
          return "&#" + r.charCodeAt(0) + ";"
        }).replace(/ /g, "&nbsp;").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />");
        break;
    }
  }
  // badword , 过滤敏感词
  //@text : 要过滤的文本 , 类型 :字符串
  //@words : 敏感词 ,类型,数组, 如 : ['你妹', '我丢' ,'我靠']
  // 如果 用 正则匹配, text 长度 100万,words 100万,需要 4秒!
  , badWord: function (text, words) { //# 敏感词过滤
    text = String(text || '');
    words = words || [];
    var reg = new RegExp(words.join('|'), 'g')
      , _self = this;
    return text.replace(reg, function ($0) {
      var length = String($0 || '').length;
      return _self.repeat('*', length);
    });
  }

};

加密方法集

Angela.encrypt = { //# 加密
  md5: function (words) { //# md5 哈希算法
    /*
     * Crypto-JS 3.1.2
     * http://code.google.com/p/crypto-js
     */
    var CryptoJS = function (s, p) {
      var m = {}, l = m.lib = {}, n = function () { }, r = l.Base = { extend: function (b) { n.prototype = this; var h = new n; b && h.mixIn(b); h.hasOwnProperty("init") || (h.init = function () { h.$super.init.apply(this, arguments) }); h.init.prototype = h; h.$super = this; return h }, create: function () { var b = this.extend(); b.init.apply(b, arguments); return b }, init: function () { }, mixIn: function (b) { for (var h in b) b.hasOwnProperty(h) && (this[h] = b[h]); b.hasOwnProperty("toString") && (this.toString = b.toString) }, clone: function () { return this.init.prototype.extend(this) } }, q = l.WordArray = r.extend({ init: function (b, h) { b = this.words = b || []; this.sigBytes = h != p "") }, parse: function (b) { for (var a = b.length, g = [], j = 0; j < a; j += 2) g[j > 3] |= parseInt(b.substr(j, 2), 16) << 24 - 4 * (j % 8); return new q.init(g, a / 2) } }, a = v.Latin1 = { stringify: function (b) { var a = b.words; b = b.sigBytes; for (var g = [], j = 0; j < b; j++) g.push(String.fromCharCode(a[j > 2] > 24 - 8 * (j % 4) & 255)); return g.join("") }, parse: function (b) { for (var a = b.length, g = [], j = 0; j < a; j++) g[j > 2] |= (b.charCodeAt(j) & 255) << 24 - 8 * (j % 4); return new q.init(g, a) } }, u = v.Utf8 = { stringify: function (b) { try { return decodeURIComponent(escape(a.stringify(b))) } catch (g) { throw Error("Malformed UTF-8 data"); } }, parse: function (b) { return a.parse(unescape(encodeURIComponent(b))) } },
        g = l.BufferedBlockAlgorithm = r.extend({ reset: function () { this._data = new q.init; this._nDataBytes = 0 }, _append: function (b) { "string" == typeof b && (b = u.parse(b)); this._data.concat(b); this._nDataBytes += b.sigBytes }, _process: function (b) { var a = this._data, g = a.words, j = a.sigBytes, k = this.blockSize, m = j / (4 * k), m = b "init") || (c.init = function () { c.$super.init.apply(this, arguments) }); c.init.prototype = c; c.$super = this; return c }, create: function () { var a = this.extend(); a.init.apply(a, arguments); return a }, init: function () { }, mixIn: function (a) { for (var c in a) a.hasOwnProperty(c) && (this[c] = a[c]); a.hasOwnProperty("toString") && (this.toString = a.toString) }, clone: function () { return this.init.prototype.extend(this) } }, n = j.WordArray = f.extend({ init: function (a, c) { a = this.words = a || []; this.sigBytes = c != m "") }, parse: function (a) { for (var c = a.length, b = [], d = 0; d < c; d += 2) b[d > 3] |= parseInt(a.substr(d, 2), 16) << 24 - 4 * (d % 8); return new n.init(b, c / 2) } }, g = b.Latin1 = { stringify: function (a) { var c = a.words; a = a.sigBytes; for (var b = [], d = 0; d < a; d++) b.push(String.fromCharCode(c[d > 2] > 24 - 8 * (d % 4) & 255)); return b.join("") }, parse: function (a) { for (var c = a.length, b = [], d = 0; d < c; d++) b[d > 2] |= (a.charCodeAt(d) & 255) << 24 - 8 * (d % 4); return new n.init(b, c) } }, r = b.Utf8 = { stringify: function (a) { try { return decodeURIComponent(escape(g.stringify(a))) } catch (c) { throw Error("Malformed UTF-8 data"); } }, parse: function (a) { return g.parse(unescape(encodeURIComponent(a))) } }, k = j.BufferedBlockAlgorithm = f.extend({ reset: function () { this._data = new n.init; this._nDataBytes = 0 }, _append: function (a) { "string" == typeof a && (a = r.parse(a)); this._data.concat(a); this._nDataBytes += a.sigBytes }, _process: function (a) { var c = this._data, b = c.words, d = c.sigBytes, f = this.blockSize, h = d / (4 * f), h = a "htmlcode">
Angela.date = { //# 日期时间
  //@s : 开始时间
  //@e : 结束时间
  //@n : 当前时间 , n 的格式为 毫秒数
  isInArea: function (s, e, n) { //# 判断时间区域
    var start = this.parse(s),
      end = this.parse(e),
      now = parseFloat(n) || new Date()
    ;
    start = Math.min(start, end);
    end = Math.max(start, end);
    return now >= start && now <= end "htmlcode">
Angela.browser = { //#浏览器
  browsers: { //# 浏览器内核类别
    weixin: /micromessenger(\/[\d\.]+)*/  //微信内置浏览器
    , mqq: /mqqbrowser(\/[\d\.]+)*/    //手机QQ浏览器
    , uc: /ucbrowser(\/[\d\.]+)*/      //UC浏览器
    , chrome: /("htmlcode">
Angela.json = { //# json 对象
  // 字符串 变为 json 对象
  parse: function (data) { //# 格式化字符串,变为 json 对象
    var // JSON RegExp
      rvalidchars = /^[\],:{}\s]*$/
      , rvalidbraces = /("\\\/bfnrt]|u[\da-fA-F]{4})/g
      , rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-"string") {
      data = data.replace(/^\s+|\s+$/g, '');
      if (data && rvalidchars.test(data.replace(rvalidescape, "@")
        .replace(rvalidtokens, "]")
        .replace(rvalidbraces, ""))) {
        return (new Function("return " + data))();
      }
    }
    return '';
  }
};

extend方法

Angela.extend = function () { //# 对象扩展
  var target = arguments[0] || {}
    , i = 1
    , length = arguments.length
    , options
  ;
  if (typeof target != "object" && typeof target != "function")
    target = {};
  for (; i < length; i++) {
    if ((options = arguments[i]) != null) {
      for (var name in options) {
        var copy = options[name];
        if (target === copy) {
          continue;
        }
        if (copy !== undefined) {
          target[name] = copy;
        }
      }
    }
  }
  return target;
};

类型判断的方法

/*
*判断变量val是不是整数类型
*/
function isNumber(val) {
  return typeof val === 'number' && isFinite(val);
}

/*
*判断变量val是不是布尔类型
*/
function isBoolean(val) {
  return typeof val === 'boolean';
}

/*
*判断变量val是不是字符串类型
*/
function isString (val) {
  return typeof val === 'string';
}

/*
*判断变量val是不是undefined
*/
function isUndefined(val) {
  return typeof val === 'undefined';
}

/*
*判断变量val是不是对象
*/
function isObj(str) {
  if (str===null||typeof str==='undefined') {
    return false;
  }
  return typeof str === 'object';
}

/*
*判断变量val是不是null
*/
function isNull(val) {
  return val === null;
}

/*
*判断变量arr是不是数组
*方法一
*/
function isArray1(arr) {
  return Object.prototype.toString.apply(arr) === '[object Array]';
}

/*
*判断变量arr是不是数组
*方法二
*/
function isArray2(arr) {
  if (arr === null || typeof arr === 'undefined') {
    return false;
  }
  return arr.constructor === Array;
}

标签:
JavaScript,WEB,操作方法

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“常用的JavaScript WEB操作方法分享”

暂无常用的JavaScript WEB操作方法分享的评论...