完整版的封装函数,返回元素e的第n个兄弟元素节点,n为正,返回后面的兄弟元素节点,n为负,返回前面的,n为0,返回自己
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<span></span>
<p></p>
<strong></strong>
<i></i>
</div>
<script>
var strong = document.getElementsByTagName('strong')[0];
function retSibling(e, n) {
while (e && n) {
if (n > 0) {
if (e.nextElementSibling) {
e = e.nextElementSibling;
} else {
for (var e = e.nextSibling; e && e.nodeType != 1; e = e.nextSibling);
}
n--;
} else {
if (e.previousElementSibling) {
e = e.previousElementSibling;
} else {
for (e = e = e.previousElementSibling; e && e.nodeType != 1; e = e = e.previousElementSibling);
}
n++;
}
}
return e;
}
</script>
</body>
</html>