zoukankan      html  css  js  c++  java
  • 模拟jquery

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>jquery</title>
    </head>
    <body>
    <input type="text" id="text">
    <script type="text/javascript">
    
    var wzw=function(id){
        this.ele=document.getElementById(id)
        return this;
    }
    
    // 设置class
    wzw.prototype.setClass=function(classN){
        this.ele.className=classN;
        return this;
    }
    
    // 去掉指定的class
    wzw.prototype.removeClass= function(classN){
        this.ele.className=this.ele.className.replace(classN,"");
        return this;
    };
    
    // 点击方法
    wzw.prototype.click= function(fn){
        if (this.ele.addEventListener){
            this.ele.addEventListener("click",fn,false);
        }else{
            this.ele.attachEvent("onclick",fn);
        };
        return this;
    };
    
    // hover
    wzw.prototype.hover= function(overfn,outfn){
        this.ele.onmouseover=overfn;
        this.ele.onmouseout=outfn;
        return this;
    };
    
    // 添加指定的class
    wzw.prototype.addClass= function(classN){
        this.ele.className=this.ele.className+" "+classN;
        return this;
    };
    // 判断是否有指定的class
    wzw.prototype.hasClass=function(classN){
        if (this.ele.className.match(classN)){
            return true
        }else{
            return false
        };
    }
    
    
    // 设置css
    wzw.prototype.css=function(k,v){
        if (v){
            this.ele.style[k]=v;
        }else if(!v){
            for(key in k){
            this.ele.style[key]=k[key];    
            }
        }else{
            console.log("参数错误")
        }
        return this;
    }
    
    // 使用$包裹一下
    function $(id){
        return new wzw(id)
    }
    
    
    $("text").setClass("wangzw").css({border:"2px solid #f00","400px",height:"20px",fontSize:"20px",padding:"5px 10px",outline:"none",borderRadius:"5px"}).removeClass("wangzw").hover(function(){
        this.style.background="rgba(220,230,240,.2)";
    },function(){
        this.style.background="";
    }).click(function(){
        this.value="000000"
    });
    </script>
    </body>
    </html>
  • 相关阅读:
    PHP 周转换为日期(最后一天) date("o-W")转Y-m-d日期——贴上代码
    开机后apache假死问题解决
    JS控制滚动条 —— 进度条的简单制作
    2014年初组装的第一台电脑
    开启mysql远程连接
    PHP数组的指针操作方法
    Ubuntu Server 13.04安装图形界面
    utf-8和GBK中文字符的长度计算
    2014校园招聘——历程2
    基于LLVM开发属于自己Xcode的Clang插件
  • 原文地址:https://www.cnblogs.com/busicu/p/4578565.html
Copyright © 2011-2022 走看看