复制代码 代码如下:
--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="/UploadFiles/2021-04-02/4.jpg"><div>
第四层<input id="Button1" type="button" value="显示节点" onclick="shownode();"/></div>
</body>
</html>
--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="/UploadFiles/2021-04-02/4.jpg"><div>
第四层<input id="Button1" type="button" value="显示节点" onclick="shownode();"/></div>
</body>
</html>
标签:
document,element
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“document.documentElement的一些使用技巧”评论...
更新动态
2025年05月12日
2025年05月12日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]