Dom中的nextSibling属性

xj150145223 2009-12-11 08:41:39

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function window.onload(){
var doc=document.getElementById("b1");
alert(doc); //[object HTMLIputElement]
alert(doc.nextSibling); //[object Text]
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="b1" value="1"/>
<input type="button" id="b2" value="2"/>
</div>
</form>
</body>
</html>


//用nextSibling属性无法获得<input type="button" id="b2" value="2"/>
属性,如果想用兄弟节点获得<input type="button" id="b2" value="2"/>元素,该怎么做
...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2009-12-11
  • 打赏
  • 举报
回复
var next=function(obj) {
if(obj.nextElementSibling)
return obj.nextElementSibling;
return obj.nextSibling;
}
xj150145223 2009-12-11
  • 打赏
  • 举报
回复
你那个代码提示死循环,能优化下么
fengsky491 2009-12-11
  • 打赏
  • 举报
回复
不好意思说反了,是在FF等非IE浏览器中,空格换行也是节点
IE中会忽略掉这些的,doc.nextSibling代表的就是button2
[Quote=引用 1 楼 fengsky491 的回复:]
FF中空格换行也是节点doc.nextSibling,在FF中表示的是2个button中的换行
[/Quote]
fengsky491 2009-12-11
  • 打赏
  • 举报
回复
var doc=document.getElementById("b1");
alert(doc); //[object HTMLIputElement]
alert(doc.nextSibling); //[object Text]
var next = doc.nextSibling;
if(next){
while(next.nodeType == 3){//是文本节点
next = doc.nextSibling;
}
}

alert(next.id);

fengsky491 2009-12-11
  • 打赏
  • 举报
回复
IE中空格换行也是节点doc.nextSibling,在ie中表示的是2个button中的换行

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧