jquery部分功能的实现

第一个实现的功能 获取兄弟节点 

window.jQuery = function(node){

let node;

return{
   geSiblings:function(){
 var allChildren = node.parentNode.children;
   var array = {
      length:0
     }
   for(let i =0;i<allChildren.length;i++){
   if(allChildren[i]!==node){
   array[array.length] = allChildren[i]
   array.length +=1;
     }
  }
return array
 },

}

第二个 给节点添加类  和类型检查

window.jQuery = function(nodeOrSelector){
let node;
if(typeof nodeOrSelector === 'string'){
node = document.querySelectorAll(nodeOrSelector);
}else{
node = nodeOrSelector
}
return{
geSiblings:function(){
var allChildren = node.parentNode.children;
var array = {
length:0
}
for(let i =0;i<allChildren.length;i++){
if(allChildren[i]!==node){
array[array.length] = allChildren[i]
array.length +=1;
}
}
return array
},
addClass:function(classes){
classes.forEach((value)=>node.classList.add(value))
}
}
}

最后一个功能 给元素修改文本内容

window.jQuery = function(nodeOrSelector){
let nodes;
if(typeof nodeOrSelector === 'string'){
nodes = document.querySelectorAll(nodeOrSelector)
}else if(nodeOrSelector instanceof Node){
nodes = {
0:nodeOrSelector,
length:1
}
}

nodes.text = function(text){
if(text === undefined){
var texts = []
for(let i =0;i<nodes.length;i++){
nodes[i].textContent = text
}
return texts
}else{
for(let i =0;i<nodes.length;i++){
nodes[i].textContent = text
}
}
}
return nodes;
}

posted @ 2018-04-19 02:59  阿尔萨斯的心  阅读(107)  评论(0编辑  收藏  举报