首页 > 代码库 > 封装自己的yQuery
封装自己的yQuery
function myAddEvent(obj, sEv, fn) { if (obj.attachEvent) { obj.attachEvent(‘on‘ + sEv, fn) } else { obj.addEventListener(sEv, fn, false); }}function getByClass(oParent, sClass) { var aEle = oParent.getElementByTagName(‘*‘); var aResult = []; var i = 0; for (var i = 0; i <aResult.length; i++) { aResult.push(aEle[i]); } return aResult;}function VQuery(vArg) { //用来保存选中的元素 this.elements = []; switch (typeof vArg) { case ‘function‘: //window.onload = vArg; myAddEvent(window, ‘load‘, vArg); break; case ‘string‘: switch (vArg.charAt(0)) { case ‘#‘: //ID var obj = document.getElementById(vArg.substring(1)); this.elements.push(obj); break; case ‘.‘: //class getByClass(document, vArg.substring(1)); break; default: //tagName this.elements = document.getElementsByTagName(vArg); } break; case ‘object‘: this.elements.push(vArg); break; }}VQuery.prototype.click = function (fn) { var i = 0; for (var i = 0; i < this.elements.length; i++) { myAddEvent(this.elements[i], ‘click‘, fn); }}function $(vArg) { return new VQuery(vArg);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。