节点的层次关系

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<script type="text/javascript" src="new_file.js"></script>

</head>

<body>

<input type="radio" name="" /><input type="text" name="" /><div id="box">

<span>我是span1</span>

<span>我是span2</span></div><input type="text" name="" />

<script type="text/javascript">

 

// 怎么获取前面所有的兄弟元素??????

 

 

var oDiv = $("box");

// 1.获取所有的子节点 childNodes

console.log( oDiv.innerText );

// 2.获取第一个子节点 firstChild

console.log( oDiv.firstChild );

// 3.获取最后一个子节点 lastChild

console.log( oDiv.lastChild );

// 4.获取父节点  parentNode

console.log( oDiv.parentNode );

 

////////////////兄弟关系/////////////////

// 5.获取前一个兄弟节点  previousSibling

console.log( oDiv.previousSibling );

// 6.获取下一个兄弟节点  nextSibling

console.log( oDiv.nextSibling );

 

///////////////////根节点/////////////////

console.log( oDiv.ownerDocument );

 

 

</script>

</body>

</html>

猜你喜欢

转载自www.cnblogs.com/d-z-j-boke/p/9621506.html