原型 - ancestors() 方法
此方法收集元素的所有祖先元素,并将它们作为扩展元素的数组返回。
请记住,body 和 html 元素也将包括在内。
语法
element.ancestors();
返回值
HTML 元素的数组。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showElements() {
var arr = $('kid').ancestors();
arr.each(function(node) {
alert(node.nodeName + ': ' + node.innerHTML);
});
}
</script>
</head>
<body>
<div id = "father">
<p id = "kid">This is first paragraph</p>
</div>
<br />
<input type = "button" value = "showElements" onclick = "showElements();"/>
</body>
</html>
输出
prototype_element_object.htm
广告