zoukankan      html  css  js  c++  java
  • 原生js模拟jquery写法

    function $_custom(fun)
    {
        document.onreadystatechange = function()
        {
            if (document.readyState == "complete")
            {
                fun();
            }
        }
    }
    function $(val){
        if(val.indexOf("#")==0)
        {
            var ob=new Array();
            var obj=document.getElementById(val.substring(1));
            ob.push(obj);
            custom.call(ob);
            return ob;
        }
        if(val.indexOf(".")==0)
        {
            var obj=document.getElementsByTagName("*");
            var ob=new Array();
            for(var x in obj)
            {
                if(obj[x].className==val.substring(1))
                {
                    obj2=obj[x];
                    ob.push(obj2);
                }
            }
            custom.call(ob);
            return ob;
        }
    }
    var custom=function(){
        var actions=["click","blur","focus","mouseout","mouseover","change"];
        //样式处理
        this.css=function(param){
            for(var i = 0;i < this.length;i++)
            {
                for(var key in param)
                {
                    this[i].style[key]=param[key];
                }
            }
        };
        var _this=this;
        //函数处理
        (function(){
            for(var k in actions){
                var _o=actions[k];
                _this[_o]= function(){
                    var _oo=_o;
                    return function(fun)
                    {
                        for(var i = 0;i < _this.length;i++)
                        {
                            _this[i]["on"+_oo]=function(event)
                            {
                                fun(event);
                            }
                        }
                    }
                }(_o);
            }
        })(actions);
        //还原javascript基本写法
        this.revert=function(){
            if(_this.length==1)
            {
                return _this[0];
            }
            else{
                alert("Does not support!");
                console.log("Does not support!");
            }
        };
        //模拟鼠标事件
        this.similar=function(actions){
            if(document.all) {
                _this[0][actions]();
            }
            else {
                var e = document.createEvent("MouseEvents");
                e.initMouseEvent(actions, true, true);
                _this[0].dispatchEvent(e);
            }
        };
        //绑定事件
        this.bind=function(actions,fun){
            if(document.all) {
                for(var i = 0;i < _this.length;i++)
                    _this[i].attachEvent("on"+actions,function(){fun.call(_this[i])});
            }else{
                for(var i = 0;i < _this.length;i++)
                    _this[i].addEventListener(actions,fun);
            }
        }
    }
    
    使用方法介绍
    
    样式处理
    
    <script type="text/javascript" src="$_custom.js"></script>
    <script type="text/javascript">    
    $_custom(function(){    
        $("#sp").css({"100px",height:"100px",border:"1px solid red"});
        $(".sp2").css({"100px",height:"100px",border:"1px solid red"});
     })</script>
    <div id="sp">test</div>
    <div class="sp2">test</div>
    <div class="sp2">test</div> 
    
    事件处理
    
    //支持的事件在actions数组里拓展
    $("#sp").click(function(){
      alert(′test′);
    }) 
    
    原始写法
    
    $("#sp").revert().style.display=′none′;
    
    模拟鼠标事件
    
    $("#sp").click(function(){
            alert(′clicked′);
        })
    $("#sp").similar("click");
    //这样刚进入网页即运行点击事件了
    
    事件绑定
    
    $("#sp").bind("click",function(){
            alert(′you click′);
        })
    
    获取鼠标位置
    
     $("#sp").click(function(){
          alert(event.x);
        })
    //event参数可直接支持调用,可以阻止事件冒泡等,不信试试吧
    
  • 相关阅读:
    Mybatis-plugin插件的使用
    SpringBoot整合mybatis-pagehelper实现分页
    springboot打war包
    keepalived1.4.0安装启动卸载
    六、nginx 搭建高可用集群
    五、nginx的动静分离
    四、nginx的负载均衡
    linux中mysql忘记root密码如何登陆
    十分钟到你了解OpenStack--nova组件
    docker 中 add 和 copy 的区别
  • 原文地址:https://www.cnblogs.com/hutuzhu/p/3511713.html
Copyright © 2011-2022 走看看