JS:DOM 常用属性

1.childNodes  返回包含被选节点的子节点的 NodeList
                如果选定的节点没有子节点,则该属性返回不包含节点的 NodeList。
                如需循环子节点列表,使用 nextSibling 属性,要比使用父对象的 childNodes 列表效率更高。
            2.firstChild     返回被选节点的第一个子节点,如果选定的节点没有子节点,则该属性返回 NULL。
            3.lastChild     可返回文档的最后一个子节点
            4.ownerDocument返回某元素的根元素
            5.parentNode     可返回某节点的父节点。如果指定的节点没有父节点则返回 null。
            6.previousSibling 可返回某节点之前紧跟的节点(处于同一树层级)
                        返回节点以节点对象返回。注意:如果没有此节点,那么该属性返回 null。
            7.nextSibling      可返回某个元素之后紧跟的节点(处于同一树层级中)。
                        返回节点以节点对象返回。注意: 如果元素紧跟后面没有节点则返回 null.
            
            //与之对应的查找元素节点
            1.firstElementChild 返回被选节点的第一个子元素节点,如果选定的节点没有子元素节点,则该属性返回 NULL。
            2.lastElementChild  返回被选节点的最后一个子元素节点,如果选定的节点没有子元素节点,则该属性返回 NULL。
            3.previousElementSibling 可返回某节点之前紧跟的元素节点(处于同一树层级)注意:如果没有此元素节点,那么该属性返回 null。
            4.nextElementSibling  可返回某节点之后紧跟的元素节点(处于同一树层级)注意:如果没有此元素节点,那么该属性返回 null。
            5.children  属性返回元素的子元素的集合,是一个 HTMLCollection 对象。只返回元素节点
            6.parentNode     可返回某节点的父节点。如果指定的节点没有父节点则返回 null 。

<div id="box">
			<div class="d1">
				文本
				<span>我是span</span>
				文本
				<p>ppppppppppppppp</p>
			</div>
			<div class="d2">
				文本
				<span>我是span</span>
			</div>
		</div>
		
		<script type="text/javascript">
			var d1 = document.querySelector(".d1");
			var d2 = document.querySelector(".d2");
			
			console.log("childNodes:",d1.childNodes);
			console.log("firstChild:",d1.firstChild);
			console.log("lastChild:",d1.lastChild);
			console.log("ownerDocument:",d1.ownerDocument);
			console.log("ownerDocument:",d2.ownerDocument);
			console.log("previousSibling:",d2.previousSibling);
			console.log("nextSibling:",d1.nextSibling);
			
			console.log("------------------------------------------");
			
			console.log("children:",d1.children);
			console.log("firstElementChild:",d1.firstElementChild);
			console.log("lastElementChild:",d1.lastElementChild);
			console.log("parentNode:",d1.parentNode);
			console.log("previousElementSibling:",d2.previousElementSibling);
			console.log("nextElementSibling:",d1.nextElementSibling);
			
			
			
			
			
		</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值