/***********************************index.html**********************************/
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8"/>
<script src="1.js"></script>
<title>js---DOM</title>
</head>
<body>
<div><span id="box">盒子</span>模型</div>
</body>
</html>
/*************************************js获取节点操作************************************/
window.onload = function(){
//想要操作盒子box 必须先找到box
//通过id查找
var box = document.getElementById("box");
//获取该span子节点
//alert(box.childNodes); // [object NodeList]
//获取span的父节点
//alert(box.parentNode); // [object HTMLDivElement]
//alert(box.parentNode.innerHTML); //<span id="box">盒子</span>模型
//获取下一个兄弟节点
//alert(box.nextSibling); //[object Text]
//获取下一个兄弟节点
//alert(box.nextSibling.nodeValue); //模型
//返回同胞节点
//alert(box.nextSibling.previousSibling); [object HTMLSpanElement]
//自己的根节点
//alert(box.ownerDocument); //[object HTMLDocument]
alert(box.ownerDocument===document); //true
}
Js基础节点一些操作草稿
猜你喜欢
转载自blog.csdn.net/feiyucity/article/details/86666653
今日推荐
周排行