Alice0123456789
2016-01-01 15:15
默认代码
function get_previousSibling(n){
var x=n.previousSibling;
while (x && x.nodeType!=1){
x=x.previousSibling;
}
return x;
}
var x=document.getElementsByTagName("li")[4];
document.write(x.nodeName);
document.write(" = ");
document.write(x.innerHTML);
var y=get_previousSibling(x);
if(y!=null){
document.write("<br />nextsibling: ");
document.write(y.nodeName);
document.write(" = ");
document.write(y.innerHTML);
}else{
document.write("<br>已经是最后一个节点");
}
我的代码
function get_previousSibing(m){
var z=m.previousSibing;
while(z && z.nodeType!=1){
z=z.previousSibing;
}
return z;
}
var z=document.getElementsByTagName("li")[2];
document.write("<br><br>")
document.write(z.nodeName+"="+z.innerHTML);
var d=get_previousSibing(z);
if(d!=null)
{
document.write("<br />previoussibling:"+d.nodeName+" = "+d.innerHTML);
}
else {
document.write("<br>已经是最后一个节点");
}
你的获取前节点的拼写出错,z=m.previousSibing是错误的,应该是z=m.previousSibling
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题