// JavaScript Document function myAddEvent(obj,sEv,fn){ alert(‘fn:‘+fn); if(obj.attachEvent){ obj.attachEvent(‘on‘+sEv,fn); }else{ obj.addEventListener(sEv,fn,false); } } /*getClassName 完整版*/ function getClassName(oParent,sClass){ var aEle = oParent.getElementsByTagName(‘*‘); var iResult = []; /*一般如果需要传递参数就用new RegExp,不用//,//会把里面的所有内容都当场字符串*/ var re = new RegExp(‘\\b‘+sClass+‘\\b‘); for(var i=0;i<aEle.length;i++){ if(re.test(aEle[i].className)){ iResult.push(aEle[i]); } } return iResult; } function Vquery(vArg){ this.elements = []; switch(typeof vArg){ case ‘function‘: alert(111); 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 this.elements = getClassName(document,vArg.substring(1)); break; default ://tagname this.elements = document.getElementsByTagName(vArg); break; } break; default: /*对象*/ break; } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script src=‘vQuery.js‘></script> <script> window.onload = function(){ new Vquery(function(){ alert(‘a‘); }); new Vquery(function(){ alert(‘b‘); }); }; </script> </head> <body> </body> </html>
原文:https://www.cnblogs.com/moon-yyl/p/9042768.html