原型 - 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
廣告