zoukankan      html  css  js  c++  java
  • 原生jQuery代码

    function myJquery(selector){
    if(typeof selector=="string") {
    if (selector.charAt(0) == "<" && selector.slice(-1) == ">") {
    var ele = selector.slice(1,-1);
    this[0] = document.createElement(ele);
    this.length = 1;
    } else {
    var all=document.querySelectorAll(selector);
    for (var i = 0; i < all.length; i++) {
    this[i] = all[i];
    }
    this.length = all.length;
    }
    }else if(typeof selector=="undefined"){
    return this;
    }else if(typeof selector=="function"){
    this.ready(selector);
    }else if(selector.nodeType==1){
    this[0]=selector;
    this.length=1;
    }
    }

    myJquery.prototype={
    each:function(callback){
    for (var i=0;i<this.length;i++){
    callback(i,this[i]);
    }
    },
    html:function(val){
    this.each(function(index,obj){
    obj.innerHTML=val
    });
    return this;
    },
    css:function(attr,val){
    this.each(function(index,obj){
    if (typeof attr=="object"){
    for (var i in attr){
    if (i=="width"||i=="height"||i=="margin"||i=="fontSize"){
    obj.style[i]=parseInt(attr[i])+"px";
    }
    obj.style[i]=attr[i];
    }
    }else{
    obj.style[attr]=val;
    }
    })
    return this;
    },
    ready:function(callback){
    document.addEventListener("DOMContentLoaded",function(){
    callback();
    })
    },
    click:function(callback){
    this.each(function(index,obj){
    obj.onclick=function(){
    callback.call(obj);
    }
    })
    return this;
    },
    appendTo:function(dom){
    if (typeof dom=="string"){
    var all=document.querySelectorAll(dom)
    var ele=this[0]
    for (var i=0;i<all.length;i++){
    this[i]=ele.cloneNode(true)
    all[i].appendChild(this[i])
    this.length=all.length
    }
    }else{
    dom.appendChild(this[0])
    }
    // document.querySelector(dom).appendChild(this[0]);
    return this;
    }

    }
    function $(selector){
    return new myJquery(selector);
    }
  • 相关阅读:
    ARCGIS JAVASCRIPT API (3.2)部署
    WINFORM 只能运行一个实例问题
    iOS 版本号
    同步和异步的区别
    简单的手机号判断
    "_inflateEnd", referenced from "_inflateInit_"等。这时需要在工程中加入libz.dlib 文件
    iOS 实现打电话
    assign retain copy iOS
    iOS 长按事件 UILongPressGestureRecognizer
    UITableView 滑动删除
  • 原文地址:https://www.cnblogs.com/weigaojie/p/10522801.html
Copyright © 2011-2022 走看看