思路
- 判斷傳入的參數(shù)類型禽车,選擇器則選取對應全部元素姑宽,元素則放入偽數(shù)組
- 通過DOM操作遣耍,實現(xiàn)添加class與修改內(nèi)容
<!DOCTYPE html>
<html>
<head>
<meta charset="{CHARSET}">
<title></title>
<style>
.red{color: red;}
</style>
</head>
<body>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<script>
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(classes){
classes.forEach((value)=> {
for (let i = 0;i<nodes.length;i++){
nodes[i].classList.add(value)
}
})
}
nodes.setText = function(classes){
classes.forEach((value)=> {
for (let i = 0;i<nodes.length;i++){
nodes[i].textContent = value
}
})
}
return nodes
}
window.$ = jQuery
</script>
</body>
</html>