Prototype 1.6.0+等同于'document.getElementsByClassName'是什么?

时间:2011-11-28 18:27:59

标签: javascript selector prototypejs

具体来说,我需要等效的选择器,它在Prototype 1.5.0中起作用:

//for each element with class of 'myClassName' and an ancestor with id='myElementID'...
document.getElementsByClassName('myClassName', $('myElementID')).each( ... );

我试过了:

$$('myElementID input.myClassName').each( ... ); //Because I will be selecting input elements with this class

和此:

$$('myElementID .myClassName').each( ... ); //Trying to get all child elements with this class name

我每次都会得到一个空列表。我想要的子元素不一定是直接的孩子,所以我知道>字符不起作用。

我不使用Prototype,也无法在这个问题上找到帮助。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:3)

$$函数将使用任何CSS选择器,因此请使用#来按ID获取myElementID。选择器的其余部分用于获取myClassNamemyElementID类的所有输入是正确的。

$$('#myElementID input.myClassName').each( ... );

答案 1 :(得分:3)

查看文档,看起来以下内容应该可以正常工作:

$$('#myElementID .myClassName').each( ... );

http://www.prototypejs.org/api/utility/dollar-dollar