var node = {}
if (typeof nodeOrSelector === 'string') {
var temp = document.querySelectorAll(nodeOrSelector);
for (var i = 0; i < temp.length; i++) {
node[i] = temp[i]
}
node.length = temp.length
}else if(nodeOrSelector.nodeType === 1){
node[0] = nodeOrSelector
node.length = 1
}
node.getsiblings = function() {
var childrenAll = node[0].parentNode.children;
var array = {
length: 0
};
for (var i = 0; i < childrenAll.length; i++) {
if (childrenAll[i] !== node[0]) {
array[array.length] = childrenAll[i];
array.length += 1;
}
}
return array;
}
node.addClass = function(array) {
for (key in array) {
var methodName
methodName = array[key] ? 'add' : 'remove';
for (var i = 0; i < node.length; i++) {
node[i].classList[methodName](key);
console.log('1')
}
}
}
node.text = function(text) {
if (text !== undefined) {
for (var i = 0; i < node.length; i++) {
node[i].textContent = text
}
} else {
console.log(node)
var texts = []
for (var i = 0; i < node.length; i++) {
texts.push(node[i].textContent)
}
return texts
}
}
return node
}
window.$ = jquery;
var $li = $(items2);
$li.addClass({
blue: true,
})
console.log($li.getsiblings())
node.querySelectorAll獲取到的是一個偽數(shù)組戚绕,為了以后使用方便應(yīng)該變成一個簡單的對象。
一個對象length是必須設(shè)置的項目碑宴,否則很多循環(huán)取值會出錯