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);
    }
  • 相关阅读:
    博客转载
    OD加载dll
    异常原理
    内核与用户模式
    简单HOOK流程
    进程与线程复习知识点
    网络编程基础
    001字符串与数字的互相转换,错误处理,消息泵机制,以及回调函数,注册窗口基本流程
    002WINDOW窗口相关点 控件基础
    WINDOW编程基础 API函数 总结翻译
  • 原文地址:https://www.cnblogs.com/weigaojie/p/10522801.html
Copyright © 2011-2022 走看看