(function() { var es = function(selector) { return new es.prototype.init(selector); }; es.domReady = function(fn) { document.addEventListener("DOMContentLoaded", function() { fn && fn(); }, false); }; es.each = function(obj, cb) { var ret, i = 0; if (obj.length === +obj.length) { //鸭辩 while(obj.length !== i) { ret = cb.call(obj[i], i, obj[i]); if (ret === "break") { break; } if (ret !== undefined) { return ret; } i++; } } else if (obj instanceof Object) { for (var k in obj) { if (obj.hasOwnProperty(k)) { ret = cb.call(obj[k], k, obj[k]); if (ret === "break") { break; } if (ret !== undefined) { return ret; } } } } }; es.event = function() { var events = []; var types = ["click"] return { on: function(type, cb) { var arr = events[type] = events[type] || []; if (types.indexOf(type) !== -1) { es.each(this, function(i, el) { el.addEventListener(type, cb); }); } if (arr.indexOf(cb) === -1) { arr.push(cb); } return this; }, off: function(type, cb) { var arr = events[type] = events[type] || [], i = 0; //传统事件 if (types.indexOf(type) !== -1) { es.each(this, function(i, el) { cb && el.removeEventListener(type, cb); if (!cb) { es.each(events[type], function(i, cb) { el.removeEventListener(type, cb); }); } }); } if(!cb) events[type] = []; cb && es.each(arr, function(i, fn) { if (fn === cb) { arr.splice(i, 1); return "break"; } }); return this; }, emit: function(type) { var arr = events[type] = events[type] || [], self = this, argv = arguments; es.each(arr, function(i, fn) { fn.apply(self, [].slice.call(argv, 1)); }); } } }(); es.ajax = function (opt) { var callback = opt.callback || function() {}; var type = opt.type || "GET"; var req = new XMLHttpRequest(); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); req.open(type, URL, true); req.onreadystatechange = function() { if (req.status === 200) { callback(null, req.responseText); } else { callback(new Error(req.statusText), req.response); } }; req.onerror = function() { callback(new Error(req.statusText)); }; req.send(opt.data ? opt.data : null); }; es.get = function(url, data, callback) { es.ajax({ url : url, data: data, callback : callback }); }; es.post = function(url, callback) { es.ajax({ type: "POST", data: data, callback: callback }); }; var oo = es.prototype = { constructor: es, splice: [].splice, shift: [].shift, push: [].push, length: 0, }; oo.init = function(selector) { var els; if (typeof selector === "string") { els = document.querySelectorAll(selector), els = [].slice.call(els, 0); } else if(selector instanceof HTMLElement) { els = [selector]; } else if (selector instanceof es) { return es; } else if (selector instanceof Array) { els = selector; } while(els[0]) { this.push(els.shift()); } }; oo.init.prototype = oo; oo.each = function(cb) { return es.each(this, function(i, el) { return cb.call(el, i, el); }); }; oo.is = function(name) { return es.each(this, function(i, el) { if (name.slice(0, 1) === "#") { return el.id === name.slice(1); } else if(name.slice(0, 1) === ".") { return el.classList.contains(name.slice(1)); } else { return el.tagName.toLowerCase() === name.toLowerCase() } }); }; oo.addClass = function(cls) { es.each(this, function(i, el) { el.classList.add(cls); }); return this; }; oo.removeClass = function(cls) { es.each(this, function(i, el) { el.classList.remove(cls); }); return this; }; oo.addStyle = function(cssText) { es.each(this, function(i, el) { el.style.cssText = cssText; }); return this; }; oo.next = function() { var self = this; es.each(this, function(i, el) { self[i] = el.nextElementSibling; }); return this; }; oo.prev = function() { var self = this; es.each(this, function(i, el) { self[i] = el.previousElementSibling; }); return this; }; oo.parent = function(selector) { var parent = []; es.each(this, function(i, el) { if (parent.indexOf(el.parentNode) === -1) { parent.push(el.parentNode); } }); this.length = 0; while(parent[0]) { var el = parent.shift(); if (selector) { es(el).is(selector) && this.push(el); } else { this.push(el); } }; return this; }; oo.children = function(selector) { var children = [], el; es.each(this, function(i, el) { children = children.concat([].slice.call(el.children, 0)); }); this.length = 0; while(children[0]) { el = children.shift(); if (!selector) { this.push(el); } if (selector && es(el).is(selector)) { this.push(el); } } return this; }; oo.find = function(selector) { var all = []; es.each(this, function(i, el) { all = all.concat([].slice.call(el.querySelectorAll(selector), 0)); }); this.length = 0; while(all[0]) { this.push(all.shift()); } return this; }; oo.first = function() { return es(this[0]); }; oo.last = function() { return es(this[this.length - 1]); }; oo.attr = function(key, value) { if (value === undefined) { return es.each(this, function(i, el) { return el.getAttribute(key); }); } es.each(this, function(i, el) { el.setAttribute(key, value); }); return this; }; es.insert = function (arr, pos, str) { es.each(arr, function(i, el) { el.insertAdjacentHTML(pos, str); }); }; oo.append = function(str) { if (typeof str === "string") { es.insert(this, "beforeend", str); } if (str instanceof HTMLElement) { es.each(this, function(i, el) { el.appendChild(str); return "break"; }); } return this; }; oo.after = function(str) { es.insert(this, "afterend", str); return this; }; oo.before = function(str) { es.insert(this, "beforebegin", str); return this; }; oo.prepend = function(str) { es.insert(this, "afterbegin", str); return this; }; oo.on = es.event.on; oo.off = es.event.off; oo.emit = es.event.emit; window.es = es; })();
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/78929.html
摘要:代码如下原生调用该文件中加入这一行代码参数参数时间调用该文件加入这一行代码中引入绑定到实例原型上组件中调用鄙人创建了一个群,供大家学习交流,希望和大家合作愉快,互相帮助,交流学习,以下为群二维码 代码如下: const slider = (function() { var Slider = {}; // the constructed function,timeManager,...
摘要:代码如下原生调用该文件中加入这一行代码参数参数时间调用该文件加入这一行代码中引入绑定到实例原型上组件中调用鄙人创建了一个群,供大家学习交流,希望和大家合作愉快,互相帮助,交流学习,以下为群二维码 代码如下: const slider = (function() { var Slider = {}; // the constructed function,timeManager,...
摘要:站在这个时间点上,我对自己之前三次失败的面试经历做了一次深度回顾。关于我第三次面试失败的经历,依然是与轮播图有关。当然,这次思特奇面试之旅,最后也是以失败告终,这也是我离进大厂最近的一次。 showImg(https://segmentfault.com/img/bVYQuP?w=528&h=513); 前言 时间的齿轮已经来到了2017年的11月份,距离2018年仅仅还剩下不到两...
阅读 1788·2021-10-09 09:44
阅读 3367·2021-09-28 09:35
阅读 1346·2021-09-01 10:31
阅读 1639·2019-08-30 15:55
阅读 2667·2019-08-30 15:54
阅读 898·2019-08-29 17:07
阅读 1350·2019-08-29 15:04
阅读 1979·2019-08-26 13:56