编写了get_previousSibling()函数,能输出选中的节点,不能输出兄弟节点,问题一直解决不了,求大神指导!
如题,代码如下:
<!doctype <!DOCTYPE html>
<html>
<head>
<mata content="text/html;charset=utf-8">
<title>previousSibing返回前一个兄弟节点</title>
</head>
<body>
<ul>
<li>java</li>
<li id="javascript">javascript</li>
<li>PHP</li>
</ul>
<ul>
<li>jquery</li>
<li>ajax</li>
<li>bootstrap</li>
</ul>
<script type="text/javascript">
function get_previousSibling(n){
var x=n.previousSibing;
while(x && x.nodeType!=1){
x=x.previousSibing;
}
return x;
}
var x=document.getElementById("javascript");
document.write(x.nodeName+'<br/>');
document.write(x.innerHTML);
var y=get_previousSibling(x);
document.write(y.nodeName+'<br/>');
document.write(y.innerHTML);
</script>
</body>
</html>
打开浏览器显示:
java
javascript
PHP
jquery
ajax
bootstrap
LI
javascript
究竟是哪出问题了???