效果图
源码
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<style>
#box p:nth-child(2n){
background-color:pink;
}
</style>
</head>
<body>
<div id="box">
<p>我是box下面的p</p>
<p>我是box下面的p</p>
<p>我是box下面的p</p>
<p>我是box下面的p</p>
<p>我是box最后面的面的p</p>
</div>
</body>
</html>
<script>
$(function(){
alert(12)
})
var a = $('#box p:nth-child(2n)').css('border','1px solid red').html('链式添加html');
$('#box p:last-child').css('backgroundColor','yellow').html('测试链式操作');
console.log(a)
function $(option){
//判断是函数还是字符串
var type = typeof option;
//转化为小写
if(type.toLowerCase() == 'function'){
console.error('参数不能是函数')
}else if(type.toLowerCase() == 'string'){
var el = document.querySelectorAll(option);
// console.log(el)
}
var obj = {
css:function(attr,value){
for(var i = 0,item;item = el[i++];){
el[i -1].style[attr] = value;
}
return obj;
},
html:function(val){
for(var i = 0,item; item = el[i++];){
el[i -1].innerHTML = val;
}
return obj;
}
}
return obj;
}
</script>