jquery中nextAll()、prevAll()方法在原生js中如何实现?
1个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RunJS 演示代码</title>
<script>
HTMLElement.prototype.prevAll = function(){
var pe = this.parentElement;
var cs = pe.children;
var arr = [];
for(var i=0;i<cs.length;i++){
var csi = cs[i];
if(csi == this){
break;
}
arr.push(csi);
}
return arr;
}
HTMLElement.prototype.nextAll = function(){
var pe = this.parentElement;
var cs = pe.children;
var arr = [];
for(var i=cs.length-1;i>=0;i--){
var csi = cs[i];
if(csi == this){
break;
}
arr.unshift(csi);
}
return arr;
}
var ck = function(dom){
console.log("prevAll=", dom.prevAll());
console.log("nextAll=", dom.nextAll());
}
</script>
</head>
<body>
<ul>
<li><a>aaa</a></li>
<li><a>bbb</a></li>
<li><a>ccc</a></li>
<li onclick="ck(this)"><a>ddd</a></li>
<li><a>eee</a></li>
<li><a>fff</a></li>
<li><a>ggg</a></li>
<li><a>hhh</a></li>
</ul>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询