Skip to content

Commit 080f25d

Browse files
committed
find
1 parent 8ed72e6 commit 080f25d

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

version/src/节点遍历.html

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</head>
1010
<body>
1111

12+
1213
<ul class="level-1">
1314
<li class="item-i">I</li>
1415
<li class="item-ii">II
@@ -27,24 +28,27 @@
2728
<li class="item-iii">III</li>
2829
</ul>
2930

31+
<script type="text/javascript">
3032

31-
<ul>
32-
<li class="first">list item 1</li>
33-
<li>list item 2</li>
34-
<li class="third-item">list item 3</li>
35-
<li>list item 4</li>
36-
<li class="end">list item 5</li>q
37-
</ul>
33+
var li = $('.item-ii');
34+
35+
console.log( li.find('li') )
3836

3937

40-
<script type="text/javascript">
4138

42-
console.log(document)
39+
function sibling(n, elem) {
40+
var matched = [];
41+
for (; n; n = n.nextSibling) { //如果存在下一个兄弟节点
42+
if (n.nodeType === 1 && n !== elem) { //是元素节点,且不是当前选择器元素
43+
matched.push(n);
44+
}
45+
}
46+
return matched;
47+
}
4348

44-
console.log( document.querySelectorAll('.first')[0] )
49+
var ul = document.querySelectorAll('.level-2')[0];
4550

4651

47-
console.log( document.body.appendChild( document.querySelectorAll('.first')[0]) )
4852

4953
</script>
5054

version/src/节点遍历sibling.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
5+
<title>参考对照学习</title>
6+
<script src="core.js"></script>
7+
<script src="sizzle.js"></script>
8+
<script src="other.js"></script>
9+
</head>
10+
<body>
11+
12+
13+
<ul>
14+
<li>list item 1</li>
15+
<li>list item 2</li>
16+
<li class="third-item">list item 3</li>
17+
<li>list item 4</li>
18+
<li>list item 5</li>
19+
</ul>
20+
21+
<script type="text/javascript">
22+
23+
24+
function sibling(n, elem) {
25+
var matched = [];
26+
for (; n; n = n.nextSibling) {
27+
if (n.nodeType === 1 && n !== elem) {
28+
matched.push(n);
29+
}
30+
}
31+
return matched;
32+
}
33+
34+
var thirdItem = document.querySelectorAll('third-item')[0]
35+
36+
var results = sibling(thirdItem.parentNode.firstChild, thirdItem)
37+
38+
console.
39+
40+
// console.log( document.body.appendChild( document.querySelectorAll('.first')[0]) )
41+
42+
</script>
43+
44+
</body>
45+
</html>

0 commit comments

Comments
 (0)