鄙人代码,希望指点(可以运行,只是感觉不好)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>nextSibling</title>
</head>
<body>
<ul id="u1">
<li id="a">javascript</li>
<li id="b">jquery</li>
<li id="c">html</li>
</ul>
<ul id="u2">
<li id="d">css3</li>
<li id="e">php</li>
<li id="f">java</li>
</ul>
<input type="text" id="pSibling" value=""/>
<input type="button" value="输入节点的下一个节点" onclick="go()">
<br/>
<script type="text/javascript">
function get_nextSibling(n){
var x=n.nextSibling;
while (x && x.nodeType!=1){
x=x.nextSibling;
}
return x;
}
var x=document.getElementsByTagName("li")[0];
document.write(x.nodeName);
document.write(" = ");
document.write(x.innerHTML);
var y=get_nextSibling(x);
if(y!=null){
document.write("<br />nextsibling: ");
document.write(y.nodeName);
document.write(" = ");
document.write(y.innerHTML);
}else{
document.write("<br>已经是最后一个节点");
}
function get_pS(idName){
console.log(idName);
var n=document.getElementsByTagName("li")[idName];
document.write(n.nodeName);
document.write(" = ");
document.write(n.innerHTML);
// console.log(n);
var x = n.previousSibling;
while(x && x.nodeType!=1)
{
x = x.previousSibling;
}
if(x!=null){
document.write("<br />previoussibling: ");
document.write(x.nodeName);
document.write(" = ");
document.write(x.innerHTML);
}else{
document.write("<br>已经是第一个节点");
}
}
function go()
{
var idwhere = parseInt(document.getElementById("pSibling").value)-1;
//必须写函数里面,调用的时候再执行,不然先获取的就是空白的,及NaN
get_pS(idwhere);
}
</script>
</body>
</html>