返回到:Prototype – 实用方法
$$() 方法解析一个或多个 CSS 过滤表达式,类似于用于定义 CSS 规则的表达式,并返回与这些过滤器匹配的元素。
语法
$$(cssRule...);
返回值
一组 HTML 元素。
例子
这是一种编写 Javascript 语句以获取名称为 div 的 DOM 的所有节点的旧方法。
nodes = document.getElementsByTagName('div');
使用 $$(),我们可以将其缩短如下 –
nodes = $$('div');
以下与 $(‘contents’) 相同,只是它返回一个数组。
$$('#contents');
例子
<html>
<head>
<title>Prototype examples</title>
<script type="text/javascript" src = "/javascript/prototype.js"></script>
<script>
function test() {
allNodes = $$("div");
for(i = 0; i < allNodes.length; i++) {
alert(allNodes[i].innerHTML);
}
}
</script>
</head>
<body>
<div id = "firstDiv" name = "div">
<p>This is first paragraph</p>
</div>
<div id = "secondDiv" name = "div">
<p>This is another paragraph</p>
</div>
<input type = "button" value = "Test $()" onclick = "test();"/>
</body>
</html>更多例子
以下返回 ID 为“contents”的元素内具有 rel 属性的所有链接。
$$('#contents a[rel]');
以下返回 href 属性值为“#”的所有链接(eyeew!)。
$$('a[href="#"]');
以下返回 ID 为“navbar”或“sidebar”的元素内的所有链接。
$$('#navbar a', '#sidebar a');
Following 返回所有链接,不包括 rel 属性包含单词“nofollow”的链接。
$$('a:not([rel~=nofollow])');
以下返回所有表体中的所有偶数行。
$$('table tbody > tr:nth-child(even)');
以下返回所有没有内容的 DIV(即,仅空白)。
$$('div:empty');返回到:Prototype – 实用方法
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8957.html
支付宝
微信