节点对象的其他属性

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>节点对象的其它常用属性</title>
</head>
<body>
	<div class="previous">我是上个兄弟</div>
	<div id="father">
		<p>mjj</p>
		<p>mjj2</p>
	</div>
	<div class="sibling">我是下个兄弟</div>

	<script type="text/javascript">
		var oFather = document.getElementById('father');
		console.log(oFather.childNodes);
		// console.log(oFather.childNodes[0]);
		// console.log(oFather.firstChild);
		// console.log(oFather.childNodes[oFather.childNodes.length - 1]);
		// console.log(oFather.lastChild);
		// console.log(oFather.parentNode.parentNode);
		console.log(oFather.nextSibling);
		// console.log(oFather.previousSibling);

		function get_childNodes(fatherNode){
			var nodes = fatherNode.childNodes;
			var arr = [];//保存已经获取的元素节点对象
			for(var i = 0; i < nodes.length; i++){
				if (nodes[i].nodeType === 1) {
					arr.push(nodes[i]);
				}
			}
			return arr;

		}
		var childnodes = get_childNodes(oFather);
		console.log(childnodes[0]);

		function get_nextSibling(n){
			var x = n.nextSibling;
			while(x  && x.nodeType != 1){
				x = x.nextSibling;
			}
			return x;
		}
		console.log(get_nextSibling(oFather));
		

	</script>
</body>
</html>
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值