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>
相关推荐
还在忙碌的吴小二27 分钟前
Harness 最佳实践:Java Spring Boot 项目落地 OpenSpec + Claude Code
java·开发语言·spring boot·后端·spring
liliangcsdn27 分钟前
mstsc不在“C:\Windows\System32“下在C:\windows\WinSxS\anmd64xxx“问题分析
开发语言·windows
小陈工39 分钟前
2026年4月7日技术资讯洞察:下一代数据库融合、AI基础设施竞赛与异步编程实战
开发语言·前端·数据库·人工智能·python
KAU的云实验台41 分钟前
【算法精解】AIR期刊算法IAGWO:引入速度概念与逆多元二次权重,可应对高维/工程问题(附Matlab源码)
开发语言·算法·matlab
Cobyte1 小时前
3.响应式系统基础:从发布订阅模式的角度理解 Vue2 的数据响应式原理
前端·javascript·vue.js
会编程的土豆1 小时前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
竹林8181 小时前
从零到一:在React前端中集成The Graph查询Uniswap V3池数据实战
前端·javascript
jerryinwuhan1 小时前
RDD第二次练习
开发语言·c#
wechat_Neal1 小时前
Golang的车载应用场景
开发语言·后端·golang