开发者

Prototype $$ returns array, should return one element like $

开发者 https://www.devze.com 2023-01-17 07:00 出处:网络
When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but s

When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?

Changing the structure of the source is开发者_运维知识库 not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.


You can also do

$$('.foo').first()

It looks cleaner than $$('.foo')[0] for my taste :)


It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.

$$('.foo')[0]
0

精彩评论

暂无评论...
验证码 换一张
取 消