原型 $$ 返回数组,应该返回一个元素,如 $

发布于 2024-09-24 03:16:45 字数 144 浏览 1 评论 0原文

当在原型中使用美元函数时,我总是得到一组元素,而不是只有一个带有美元函数的元素。如何结合 $$ 的 CSS 选择器的强大功能,但仍然只能返回一个元素?

更改源的结构是不可能的,所以我不能只用 id 选择它。它需要使用 CSS 进行选择,但应该只返回一个元素。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-10-01 03:16:45

你也可以这样做

$('.foo').first()

它看起来比 $$('.foo')[0] 更符合我的口味:)

You can also do

$('.foo').first()

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

晨曦÷微暖 2024-10-01 03:16:45

按类名选择时返回单个元素是没有意义的,因为 DOM 中可能有许多元素具有此类。因此,如果您确定返回数组的第一个元素是唯一的,则始终可以使用它。

$('.foo')[0]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文