$$ 在 Prototype 中是什么意思以及它在 Jquery 中的等价物是什么 [英] What does $$ mean in Prototype and what is its equivalent in Jquery

查看:39
本文介绍了$$ 在 Prototype 中是什么意思以及它在 Jquery 中的等价物是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Rails 应用程序从原型迁移到 Jquery,我看到类似

I am migrating an rails application from prototype to Jquery, i am seeing a syntax like

var subEl = $$("#items_grid_container")[0]

subEl.style.height = windowGridContainerEm + "em"

我知道如何将第二部分转换为 Jquery,但如何转换

I know how to convert the second part to Jquery, but how do i convert

var subEl = $$("#items_grid_container")[0]

到它在 Jquery 中的等价物.

to its equivalent in Jquery.

我尝试了 $("#items_grid_container")[0] 但它不起作用,任何帮助将不胜感激.谢谢.

I tried $("#items_grid_container")[0] but it is not working, any help would be appreciated. Thanks.

推荐答案

在原型中 $$ 用于通过 CSS 选择器获取集合.那将是逗号分隔的字符串.所以,我会说它在内部使用 document.querySelectorAll("tag/id/class/attributes").虽然 id 选择器只返回一个元素.

In prototype $$ is used to get the collection through CSS selectors. That would be a comma separated strings. So, I would say it internally uses document.querySelectorAll("tag/id/class/attributes"). Although id selector returns only one element.

所以,jQuery 等价物只是一个 $:

So, jQuery equivalent is just with single $:

$("#item_grid_container")[0]

将返回 DOM 元素而不是 jQuery 对象.因此,jQuery 方法不能应用于 DOM 元素,反之亦然.只有 jQuery 对象可以使用 jQuery 方法.如果您需要 jQuery 对象,那么您不必在选择器中添加 [0].

Will return you the DOM element instead of jQuery object. So, jQuery methods cannot be applied on DOM elements and vice versa. Only jQuery object can use the jQuery methods. If you need jQuery object then you don't have to add [0] in the selector.

所以你必须这样做:

 var el = $("#item_grid_container");

 el.width(thevarwidth);

这篇关于$$ 在 Prototype 中是什么意思以及它在 Jquery 中的等价物是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆