首页 > Web开发 > 详细

js Vquery

时间:2018-05-15 20:06:58      阅读:125      评论:0      收藏:0      [点我收藏+]

// 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>

 

js Vquery

原文:https://www.cnblogs.com/moon-yyl/p/9042768.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!