返回到:Prototype – 元素对象
此方法检查元素是否是祖先的后代。
由于 Element.descendantOf 在内部将 $() 应用于祖先,它无差别地接受一个元素或一个元素的 id 作为它的第二个参数。
语法
element.descendantOf(ancestor);
返回值
如果它发现该元素是祖先的后代,则返回 true,否则返回 false。
例子
HTML
x
35
35
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function isDescendant() {
8
var father = $('father');
9
var kid = $('kid');
10
11
// This is correct relationship and will be printed
12
if( kid.descendantOf(father) ) {
13
alert( "Kid is descendant of father" );
14
}
15
16
// This is wrong relationship and will not be printed
17
if( father.descendantOf(kid) ) {
18
alert( "Father is descendant of kid" );
19
}
20
}
21
</script>
22
</head>
23
24
<body>
25
<p>Click isDescendant button to see the result.</p>
26
<div id = "grandfather">
27
<div id = "father">
28
<div id = "kid"></div>
29
</div>
30
</div>
31
<br />
32
33
<input type = "button" value = "isDescendant" onclick = "isDescendant();"/>
34
</body>
35
</html>
返回到:Prototype – 元素对象
阅读剩余 83%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9129.html