javascript选择器的封装,只需要写元素名或css类及id都可以选择到元素

//模仿jquery选择器样式,只需要写元素名或css类及id都可以选择到元素

html 复制代码
<html>
    <head>
        <meta http-equiv="Content-Type:text/html;charset=utf=8"/>
        <link rel="shortcut icon" href="#"/>
        <title>封装选择器</title>
       
    </head>
    <style type="text/css">
        .context 
        {
            color:black;
            font-size: 16px;
           
            width: 300px;
            height: 300px;
        }
        .div
        {
            border: 1px solid blue;
        }


    </style>

    <body>
        <div class="context div" >这是文本内容

        <ul>
                <li class="listyle"><input type="text" name="user" value=""/></li>
                <li class="listyle"><input type="password" name="pwd" value=""/></li>
                <li class="listyle"><input type="password" name="pwd1" value=""/></li>

        </ul>
        </div>
        
        <script type="text/javascript">
            
            Function.prototype.addMethods=function(name,fn)
            {
                this.prototype[name]=fn;
            }

            function selectEle(selector)
            {
               var selector=this.trim(selector);
               this.elements=[];
               var node;
               if(selector.indexOf(' ')!==-1)
               {

                   node=this.getClass(selector)[0];
                   
                   if(node)
                   {
                    this.elements.push(node);
                   }else if(node=this.getTagName(selector))
                   {
                    if(node.length>1)
                    {
                        for(var i=0;i<node.length;i++)
                        {
                            this.elements.push(node[i]);
                        }
                    }
                    
                   }else
                   {
                    throw "请输入正确的选择符";
                   }
                   
               }else if(selector.indexOf('[')!==-1)
               {
                    var selector=selector.slice(1,-1);
                    node=this.getAttr(selector);
                    //console.log(this.getAttr(selector));
                    if(node)
                    {
                        this.elements.push(node);
                    }else
                    {
                        alert("请输入正确的选择符");
                    }
               }
               else
               {
                    if(/^[#|.]/.test(selector))
                    {
                        node=this.getCss(selector);
                        if(node)
                        {
                            this.elements.push(node);
                        }
                        else
                        {
                            alert("请输入正确的选择符");
                        }
                    }else
                    {
                    
                        node=this.getTagName(selector);
                        
                        if(node.length==1)
                        {
                            this.elements.push(node[0]);
                        }else if(node.length>1)
                        {
                            for(var i=0;i<node.length;i++)
                            {
                                this.elements.push(node[i]);
                            }
                        }else
                        {
                            alert('请输入正确的选择符');
                        }
                    
                    }   
               }
               
              return this.elements;
                
            }
            selectEle.addMethods('getClass',function(selector){
                return document.getElementsByClassName(selector);
            });

            selectEle.addMethods('getCss',function(selector){
                try{
                    return document.querySelector(selector);
                }catch(err)
                {
                    alert(err);
                }
                
            });

            selectEle.addMethods('getTagName',function(selector){
                try{
                    
                    return document.querySelectorAll(selector);
                }catch(err)
                {
                    alert(err);
                }
                
            });

            selectEle.addMethods('getAttr',function(selector){
                try
                {
                    var tag=document.body.getElementsByTagName('*');
                    var ele=[];
                    if(selector.indexOf('=')==-1)
                    {

                        for(var i=0;i<tag.length;i++)
                    {
                        
                        if(tag[i].getAttribute(selector)!=undefined && tag[i].tagName !="SCRIPT")
                        {
                            ele.push(tag[i]);
                        }
                        
                    }
                    
                    }else
                    {
                        var selector=selector.split('=');
                        for(var i=0;i<tag.length;i++)
                        {
                            var att=tag[i].getAttribute(selector[0]);
                            //console.log(att);
                            if(att && att!=="text/javascript" && att!=="text/css")
                            {
                                
                                if(att.toString().toLocaleLowerCase()===selector[1].toString().toLocaleLowerCase())
                                {
                                    ele.push(tag[i]);
                                }
                            }
                        }

                    }
                    return ele;
                }catch(err)
                {
                    alert(err);
                }
                    
            });

            //只是为测试IE7/8写的临时兼容去空白函数
           selectEle.addMethods('trim',function(str){
            return str.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
           });
          
            var $=function(args)
            {
                return new selectEle(args);
            }
            
         


           console.log($('li'));
           
       </script>  
    </body>
</html>
相关推荐
会跑的葫芦怪3 小时前
若依Vue 项目多子路径配置
前端·javascript·vue.js
兩尛3 小时前
c++知识点2
开发语言·c++
fengfuyao9853 小时前
海浪PM谱及波形的Matlab仿真实现
开发语言·matlab
xiaoye-duck3 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl
xiaoqi9224 小时前
React Native鸿蒙跨平台如何进行狗狗领养中心,实现基于唯一标识的事件透传方式是移动端列表开发的通用规范
javascript·react native·react.js·ecmascript·harmonyos
jin1233224 小时前
React Native鸿蒙跨平台剧本杀组队消息与快捷入口组件,包含消息列表展示、快捷入口管理、快捷操作触发和消息详情预览四大核心功能
javascript·react native·react.js·ecmascript·harmonyos
Hx_Ma164 小时前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet
期待のcode5 小时前
原子操作类LongAdder
java·开发语言
烬头88216 小时前
React Native鸿蒙跨平台实现二维码联系人APP(QRCodeContactApp)
javascript·react native·react.js·ecmascript·harmonyos
pas1366 小时前
40-mini-vue 实现三种联合类型
前端·javascript·vue.js