window.$ = function (selectorOrElement) {
var elements;
if (typeof selectorOrElement === "string") {
elements = document.querySelectorAll(selectorOrElement);//伪数组,有length对象
} else if (selectorOrElement instanceof Element) {
elements = { 0: selectorOrElement, length: 1 };//假数组
} else if (selectorOrElement instanceof Array) {
elements = {};
for (var i = 0; i < selectorOrElement.length; i++) {
elements[i] = selectorOrElement[i];
}
elements.length = selectorOrElement.length;
}
//添加事件
elements.on = function (eventType, fn) {
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.addEventListener) {
element.addEventListener(eventType, fn);
} else if (element.attachEvent) {
element.attachEvent('on' + eventType, fn);
}
}
return elements;
};
//添加类
elements.addClass = function (className) {
for (var i = 0; elements.length; i++) {
if (elements[i].classList) {//其它浏览器
elements[i].classList.add(className);
} else {//IE
elements[i].className = elements[i].className + ' ' + className;
}
}
return elements;
};
//移除类
elements.removeClass = function (className) {
for (var i = 0; i < elements.length; i++) {
if (elements[i].classList) {//其它浏览器
elements[i].classList.remove(className);
} else {/*IE需要正则表达式,暂时不会*/ }
}
return elements;
};
//设置内容
elements.text = function (string) {
if (arguments.length === 0) {//string===undefined
let result = [];
for (let i = 0; i < elements.length; i++) {
if (elements[i].textContent !== undefined) {
result.push(elements[i].textContent);
} else if (elements[i].innerText !== undefined) {
result.push(elements[i].innerText);
}
}
return result;
} else {
for (let i = 0; i < elements.length; i++) {
if (elements[i].textContent !== undefined) {//w3c
elements[i].textContent = string;
} else if (elements[i], innerText !== undefined) {//IE
elements[i].innerText = string;
}
}
return elements;
}
};
//设置html
elements.html = function (string) {
if (arguments.length === 0) {//string===undefined
let result = [];
for (let i = 0; i < elements.length; i++) {
result.push(elements[i].innerHTML);
}
return result;
} else {
for (let i = 0; i < elements.length; i++) {
elements[i].innerHTML = string;
}
return elements;
}
};
//获取指定索引成员
elements.get = function (index) {
return elements[index];
};
//获取所有元素的所有兄弟
elements.siblings = function () {
var result = [];
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var children = element.parentNode.children;
for (var j = 0; j < children.length; j++) {
if (children !== element) {
result.push(children[j]);
}
}
}
var objectResult = $(result);
return objectResult;
};
//返回伪数组对象
return elements;
};
var items = $('li');
items.on('click', function () {
console.log('click');
});
items.on('dblclick', function () {
console.log('双击');
items.addClass('active');
});
//items.text("ww");
//console.log(items.text());
//console.log(items.html('<p>aa</p>'));
items[0].style.border = "1px solid";
items.get(1).style.border = '1px solid blue';
var item = $(items[2]);
console.log(items.siblings());
item.siblings().addClass('active');
</script>
jquery库
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...