【学】jQuery的源码思路4——增加一些功能

本文说一些简单的jQuery实现原理

  • eq()
  • get()
  • hide()
  • show()
  • index()
  • find()
//返回找到的一组元素中的第n个
zQuery.prototype.eq=function(n){
    return $(this.elements[n]);
};

//将jQ元素改成原生的,其实就是取出jQ取得的元素数组中的n个
zQuery.prototype.get=function(n){
    return this.elements[n];
};

//隐藏
zQuery.prototype.hide=function(){
   for(var i=0;i< this.elements.length;i++){
       this.elements[i].style.display='none';
   }
};

//显示
zQuery.prototype.show=function(){
    for(var i=0;i< this.elements.length;i++){
        this.elements[i].style.display='block';
    }
};

//找到该元素的父级下的一级子元素中,它的下标
zQuery.prototype.index=function(){
    var oParent=this.elements[0].parentNode;
    var aChild=oParent.children;
    for(var i=0;i<aChild.length;i++){
        if(aChild[i]==this.elements[0]){
            return i;
        }
    }
};

//无论是第几层子级,找出对应的元素,这里涉及到要修改getEle函数
zQuery.prototype.find=function(str){
    return $(getEle(str,this.elements));
//            this.elements   ==  aParent
    //1修改getEle函数,多接受一个aParent
    //2修改getEle函数里面的aParent=aParent||[document]
    //3.修改zquery构造函数,判断object可以是多个
};
原文地址:https://www.cnblogs.com/bluefantasy728/p/5789324.html