显示Cannot read property 'previousSibling' of undefined?
<!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>
<script type="text/javascript">
function get_previousSibling(n){
var x=x.previousSibling;
while(x&&x.nodeType!=1){
x=x.previousSibling;
}
return x;
}//构造一个判断x的previousSibling是不是元素节点的函数
x=document.getElementsByTagName("li")[2];
document.write(x.nodeName+"="+x.innerHTML);
var y=get_previousSibling(x);
if(y!=null){
document.write(x+"的上一个元素节点:"+y.nodeName+"="+y.innerHTML);
}
else{
document.write(x+"是最后一个元素节点");
}
</script>
</body>
</html>
为什么输出上一个节点就不行了?哪里出问题了