• 代码片段整理


    常用代码片段整理:

      1 var common = {
    2 addEvent: function(obj, evt, fn) {
    3 try {
    4 obj.attachEvent("on" + evt, fn);
    5 } catch(e) {
    6 obj.addEventListener(evt, fn, false);
    7 }
    8 return obj;
    9 },
    10 removeEvent: function(obj, evt, fn) {
    11 try {
    12 obj.detachEvent("on" + evt, fn);
    13 } catch(e) {
    14 obj.removeEventListener(evt, fn, false);
    15 }
    16 return obj;
    17 },
    18 //-----
    19 clickOut: function(obj, fn) {
    20 var __self__ = this;
    21 this.addEvent(document, "click", function(e) {
    22 var el = e.target;
    23 while (el.parentNode) {
    24 if (el.parentNode === obj || el === obj) {
    25 return false;
    26 }
    27 el = el.parentNode;
    28 }
    29 __self__.removeEvent(document, "click", arguments.callee);
    30 setTimeout(fn);
    31 });
    32 return obj;
    33 },
    34 //获得下一节点
    35 nextNode: function(node) {
    36 if (node.nextSibling && node.nextSibling.nodeType === 1) {
    37 return node;
    38 }
    39 if (node.nextSibling) {
    40 return arguments.callee(node.nextSibling);
    41 }
    42 return node;
    43 },
    44 //创建标签
    45 createTag: function(tag, css, attr) {
    46 var node = document.createElement(tag);
    47 for (var a in css) {
    48 node.style[a] = css[a];
    49 }
    50 for (var b in attr) {
    51 node.setAttribute(b, attr[b]);
    52 }
    53 return node;
    54 },
    55 //取得get请求url参数
    56 getQuery: function(name) {
    57 var rel = window.location.search,
    58 mat = new RegExp("(?:^|&)" + name + "=([^&]*)", "i");
    59 if (rel.indexOf("?") !== - 1) {
    60 rel = rel.substr(1);
    61 }
    62 var results = rel.match(mat);
    63 return !! results ? results[1] : null;
    64 },
    65 //深度复制
    66 extend: function(source, obj) {
    67 for (var a in obj) {
    68 if (typeof obj[a] === "object") {
    69 if (source[a] == undefined) {
    70 if (obj[a].constructor === Array) {
    71 source[a] = [];
    72 } else {
    73 source[a] = {};
    74 }
    75 }
    76 arguments.callee(source[a], obj[a]);
    77 } else {
    78 source[a] = obj[a];
    79 }
    80 }
    81 return source;
    82 },
    83 //img队列,确保所有img加载完毕执行回调
    84 imgQueue: function(cache, callback, imgs) {
    85 var imgs = imgs || [],
    86 one = cache.shift(),
    87 __self__ = arguments.callee,
    88 img = new Image();
    89 img.src = one.src;
    90 img.onload = function() {
    91 imgs.push([img, one]);
    92 if (cache.length > 0) {
    93 __self__(cache, callback, imgs);
    94 } else {
    95 if (callback) {
    96 callback(imgs);
    97 }
    98 }
    99 }
    100 },
    101 //自动获取id,保证没重复id
    102 autoId: function() {
    103 var id = "cv" + new Date().getTime() + parseInt(1000 * Math.random());
    104 if (window[id] === undefined) {
    105 return id;
    106 } else {
    107 arguments.callee();
    108 }
    109 return null;
    110 },
    111 hasClass: function(el, cls) {
    112 return el.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    113 },
    114 addClass: function(el, cls) {
    115 if (!this.hasClass(el, cls)) el.className += " " + cls;
    116 },
    117 removeClass: function(el, cls) {
    118 if (this.hasClass(el, cls)) {
    119 var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
    120 el.className = el.className.replace(reg, " ");
    121 }
    122 },
    123 }
    124 //模拟console功能,针对个别不支持console的浏览器
    125 (function() {
    126 function __console__(flag) {
    127 var dos = document.createElement("div"),
    128 flag = flag || false;
    129 if (flag) {
    130 dos.style.display = "block";
    131 } else {
    132 dos.style.display = "none";
    133 }
    134 window.onload = function() {
    135 document.body.appendChild(dos);
    136 }
    137 this.dos = dos;
    138 }
    139 __console__.prototype = {
    140 log: function(log) {
    141 this.dos.innerHTML += "<p style='line-height:150%;border-bottom:1px solid #999;display:block;padding:0;margin:0;'>" + log + "</p>";
    142 }
    143 }
    144 window.console = window.console || new __console__(true);
    145 })();



  • 相关阅读:
    异常 中断 陷阱
    关于delete字符串 需不需要加 [ ]
    关于联合体输出的问题(是否小端模式)
    String reorder
    数据库 ---5 索引 创建用户及授权 数据备份
    数据库 --- 4 多表查询 ,Navicat工具 , pymysql模块
    数据库 --- 3 行(记录)操作 单表查询
    数据库 --- 2 库 ,表
    数据库 --- 1 初始 数据库
    并发 --- 5 线程的其他方法 线程队列 线程池 协程
  • 原文地址:https://www.cnblogs.com/jiajiaobj/p/2400826.html
Copyright © 2020-2023  润新知