如有HTML情况如下
1:
<div id="div1"></div>
<div>123</div>2:
<div id="div1"></div>
<span></span>
<div>123</div>
-----------
现在要在div1里定义到他后面的那个div
我用
document.getElementById("div1").nextSibling在第一种情况OK,第二种不行
我查看文档说:Retrieves a reference to the next child of the parent for the object(定位同级下一对像)
那要如何来写才正确呢?谢谢
1:
<div id="div1"></div>
<div>123</div>2:
<div id="div1"></div>
<span></span>
<div>123</div>
-----------
现在要在div1里定义到他后面的那个div
我用
document.getElementById("div1").nextSibling在第一种情况OK,第二种不行
我查看文档说:Retrieves a reference to the next child of the parent for the object(定位同级下一对像)
那要如何来写才正确呢?谢谢
解决方案 »
- 打开页面,覆盖原页面
- 网页中使用 activex插件问题
- 请问怎么统计一个div里的字符串长度?
- 关于关闭子页面时触发父页面某个事件问题!
- jQuery validate 问题,麻烦帮帮忙!
- javascript代码如何实现打开一个新页面的同时给新页面传递两个简单的数值?
- ***********请问这个代码为什么会报错啊!***********
- 请问高手以下两种加密方式是不是一样的:JavaScript.Encode 和 JScript.Encode
- 怎么可以得到浏览器标题的信息
- jsp视频上传功能怎么实现?
- |zyciis| 为什么不是加属性而是方法:String.prototype.realLength谢谢
- 关于settimeout
while(n=n.nextSilibling && n.nodeName!="DIV");
while(n=n.nextSilibling && n.nodeName!="DIV");
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<div id="div1"> </div>
<span> </span>
<div>123 </div>
<script type="text/javascript">
<!--var nextDiv = GetNextSiblingByTagName(document.getElementById("div1"), "div");
alert(nextDiv.innerText);function GetNextSiblingByTagName(source, tagName) {
var target = source.nextSibling;
while (target) {
if (target.nodeType === 1
&& target.tagName.toLowerCase() === tagName.toLowerCase()) {
return target;
}
target = target.nextSibling;
}
} //-->
</script>
</body>
</html>
while(n.nodeName && n.nodeName!="DIV"){n=n.nextSibling}
alert(n.innerHTML)