-
Notifications
You must be signed in to change notification settings - Fork 52
/
sample040.html
34 lines (33 loc) · 1.17 KB
/
sample040.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<body>
<div id="parent2">
<div id="parent1">
<div id="parent0">
<div id="start"></div>
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script> (function ($) {
// Alerts "parent0" x4
alert($('#start').parent().attr('id'));
alert($('#start').parents('#parent0').attr('id'));
alert($('#start').parents()[0].id);
// Gets actual DOM element
alert($('#start').closest('#parent0').attr('id'));
// Alerts "parent1" x4
alert($('#start').parent().parent().attr('id'));
alert($('#start').parents('#parent1').attr('id'));
alert($('#start').parents()[1].id);
// Gets actual DOM element
alert($('#start').closest('#parent1').attr('id'));
// Alerts "parent2" x4
alert($('#start').parent().parent().parent().attr('id'));
alert($('#start').parents('#parent2').attr('id'));
alert($('#start').parents()[2].id);
// Gets actual DOM element
alert($('#start').closest('#parent2').attr('id'));
})(jQuery); </script>
</body>
</html>