jQuery 获取 设置文本 设置样式

167 阅读1分钟
window.jQuery=function(nodeOrSelector){
	let nodes={}
	if (typeof nodeOrSelector ==='string') {
		let temp=document.querySelectorAll(nodeOrSelector)
		for (let i=0; i<temp.length;i++) {
			nodes[i]=temp[i]
		}
		nodes.length=temp.length
	} else if(nodeOrSelector instanceof Node){
		nodes={ 
			0:nodeOrSelector,
			length:1
		}
	}
	
	nodes.addClass=function(classcss){
		for(let i=0;i<nodes.length;i++){
			nodes[i].classList.add(classcss)
		}
	}

	nodes.texts=function(texts){
		if (texts===undefined) {
			let texts=[]
			for(let i=0;i<nodes.length;i++){
				//texts[texts.length]=divArray[i]
				texts.push(nodes[i].textContent)
			}
			return texts
		}else{
		for(let i=0;i<nodes.length;i++){
				nodes[i].textContent=texts
			}
		}
	}
	return nodes
}
window.$=jQuery
var $div=$('div')
$div.addClass('red')
//console.log($div.texts())
$div.texts('hi')