|
@@ -0,0 +1,58 @@
|
|
|
|
+<!DOCTYPE html>
|
|
|
|
+<html lang="en">
|
|
|
|
+<head>
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
+ <title>Document</title>
|
|
|
|
+</head>
|
|
|
|
+<body>
|
|
|
|
+ <div id="div1">111111111111111</div>
|
|
|
|
+ <p id="p1">222222222222</p>
|
|
|
|
+ <h1>hahahhahhhaahhahahh</h1>
|
|
|
|
+ <ul id="ul1">
|
|
|
|
+ <li>1</li>
|
|
|
|
+ <li>2</li>
|
|
|
|
+ <li>3</li>
|
|
|
|
+ </ul>
|
|
|
|
+ <script>
|
|
|
|
+ var div1 = document.getElementById('div1')
|
|
|
|
+ var p1 = document.getElementById('p1')
|
|
|
|
+ var ul1 = document.getElementById('ul1')
|
|
|
|
+ // console.log(div1.nodeType)
|
|
|
|
+ // console.log(p1.nodeName)
|
|
|
|
+ // console.log(div1.nextElementSibling)
|
|
|
|
+ // console.log(div1.nextSibling)
|
|
|
|
+ // console.log(div1.previousSibling)
|
|
|
|
+ // console.log(div1.previousElementSibling)
|
|
|
|
+ //nextSibling 指向下一个兄弟节点
|
|
|
|
+ //nextElementSibling 指向下一个元素节点
|
|
|
|
+ //previousSibling 指向上一个兄弟节点
|
|
|
|
+ //previousElementSibling 指向上一个元素节点 如果这个节点就是第一个元素节点 那么该值为null
|
|
|
|
+ console.log(p1.nextElementSibling)
|
|
|
|
+
|
|
|
|
+ //firstChild 指向在childNodes列表中的第一个节点
|
|
|
|
+ console.log(ul1.firstChild)
|
|
|
|
+ console.log(ul1.lastChild)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ function next(elem){
|
|
|
|
+ do{
|
|
|
|
+ elem = elem.nextSibling
|
|
|
|
+ }while(elem.nodeType != 1)
|
|
|
|
+ return elem
|
|
|
|
+ }
|
|
|
|
+ console.log(next(div1))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ function first(elem){
|
|
|
|
+ elem = elem.firstChild
|
|
|
|
+ if(elem.nodeType != 1){
|
|
|
|
+ elem = next(elem)
|
|
|
|
+ }
|
|
|
|
+ return elem
|
|
|
|
+ }
|
|
|
|
+ console.log(first(ul1))
|
|
|
|
+ </script>
|
|
|
|
+</body>
|
|
|
|
+</html>
|