document.documentElement的一些使用技巧_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

document.documentElement的一些使用技巧

发布时间:2026-01-17  |  点击率:
复制代码 代码如下:
--documentElement 属性可返回文档的根节点。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function shownode() {
var ohtml = document.documentElement;
//alert(ohtml.nodeName);
//alert(ohtml.childNodes.length);
//alert(ohtml.childNodes[1].nodeName); //获取body
//var obody = ohtml.childNodes[1]; //一层层的找到div
var obody = ohtml.lastChild; //快速获取ohtml的最后一个子节点
//alert(obody.childNodes[0].nodeName);
//alert(obody.parentNode.nodeName); //根据子节点找到想要的父节点
//alert(obody.previousSibling.nodeName);
var ohead = ohtml.childNodes[0];
// alert(ohead.nextSibling.nodeName);
}
</script>
</head>
//body如果和div之间有空格,或者每个层之间有空格,会影响获取结果
<body><div id="div1">第一层</div> <div id="div2">第二层</div>
<div>第三层<img src="images/4.jpg" /></div>
<div>
第四层<input id="Button1" type="button" value="显示节点" onclick="shownode();"/></div>
</body>
</html>

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598