原型 - previousSiblings() 方法
此方法收集所有元素的上一个兄弟元素,并将其作为扩展元素的数组返回。
如果两个元素具有相同的父级,则它们是兄弟元素。因此,例如,head 元素和 body 元素就是兄弟元素(它们的父级是 html 元素)。上一个兄弟元素只是先行于文档中元素的元素。
语法
element.previousSiblings();
返回值
返回 HTML 元素的数组。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var arr = $('ida-red').previousSiblings();
arr.each(function(node) {
alert(node.nodeName + ': ' + node.innerHTML);
});
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<ul id = "fruits">
<li id = "apples">
<h3 id = "title">Apples</h3>
<ul id = "list-of-apples">
<li id = "golden-delicious">Golden Delicious</li>
<li id = "mutsu">Mutsu</li>
<li id = "mcintosh" class = "yummy">McIntosh</li>
<li id = "ida-red" class = "yummy">Ida Red</li>
</ul>
<p id = "saying">An apple a day keeps the doctor away.</p>
</li>
</ul>
<br />
<input type = "button" value = "Show Result" onclick = "showResult();"/>
</body>
</html>
输出
prototype_element_object.htm
广告