返回到:Prototype – 元素对象
此方法采用任意数量的 CSS 选择器(字符串),并返回与其中任何一个匹配的元素的扩展后代数组。
此方法与 $$() 非常相似,但可以在一个元素的上下文中使用,而不是在整个文档中使用。支持的 CSS 语法是相同的,因此请参阅 $$() 文档了解详细信息。
语法
element.select(selector...);
返回值
返回一个 HTML 元素数组。
例子
HTML
x
46
46
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
var arr = $('apples').select('[title = "yummy!"]');
9
10
// returns [h3, li#golden-delicious, li#mutsu]
11
arr.each(function(node) {
12
alert("First : " + node.nodeName + ': ' + node.innerHTML);
13
});
14
arr = $('apples').select( 'p#saying', 'li[title = "yummy!"]');
15
16
// returns [li#golden-delicious, li#mutsu, p#saying]
17
arr.each(function(node) {
18
alert("Second : " + node.nodeName + ': ' + node.innerHTML);
19
});
20
arr = $('apples').select('[title = "disgusting!"]');
21
22
// returns []
23
arr.each(function(node) {
24
alert("Third : " + node.nodeName + ': ' + node.innerHTML);
25
});
26
}
27
</script>
28
</head>
29
30
<body">
31
<p id = "test">Click the button to see the result.</p>
32
<ul id = "fruits">
33
<li id = "apples">
34
<h3 title = "yummy!">Apples</h3>
35
<ul id = "list-of-apples">
36
<li id = "golden" title = "yummy!" >Golden</li>
37
<li id = "mutsu" title = "yummy!">Mutsu</li>
38
<li id = "mcintosh">McIntosh</li>
39
<li id = "ida-red">Ida Red</li>
40
</ul>
41
<p id = "saying">An apple a day keeps the doctor away.</p>
42
</li>
43
</ul>
44
<input type = "button" value = "Click" onclick = "showResult();"/>
45
</body>
46
</html>
返回到:Prototype – 元素对象
阅读剩余 87%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/9011.html