为什么要将 [0] 放在子级 (':first') 的结果上?

发布于 2024-11-03 18:52:09 字数 236 浏览 5 评论 0原文

请解释一下这段代码中的 [0]:

$(function (){      
    var $cont = $('#cont');     
    var $el = $cont.children(':first'), el = $el[0];    
}); 

我尝试了一些“警报”来查找文本,但我真的不明白为什么我们要精确索引,而我们已经知道我们指向的是“第一个”子级该div...?

Please explain the [0] in this code:

$(function (){      
    var $cont = $('#cont');     
    var $el = $cont.children(':first'), el = $el[0];    
}); 

I tried some 'alert' to find the text, but i don't really understand why we precise the index, while we already know that we're pointing on the "first" children of the div... ?

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

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

发布评论

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

评论(5

勿忘初心 2024-11-10 18:52:09

在上面的示例中,$el 是一个包含一个元素的 jQuery 对象,该元素由 :first 选择器确保。另一方面,el 包含 $el jQuery 对象中第一个(也是唯一一个)项目的底层 DOM 元素。

您可以访问 el 变量的本机属性,例如 .innerHTML。您可以在 $el 上使用所有 jQuery 支持的操作,例如 $el.html()

In the above example, $el is a jQuery object containing one element which is ensured by the :first selector. On the other hand, el contains the underlying DOM element of the first (and only) item in the $el jQuery object.

You can access native properties of the el variable, like .innerHTML. You can use all jQuery supported operations on $el, like $el.html().

喜你已久 2024-11-10 18:52:09

jQuery 对象通常包含 DOM 节点的集合/数组。例如,如果您要迭代 jQuery 对象,例如 -

$('div').each(function() {

  //In this context, this refers to the DOM node
  //and $(this) refers to a jQuery object for that particular DOM node
  //therefore $(this)[0] == this

   this.innerHTML = 'foo';
   $(this).html('foo');
   $(this)[0].innerHTML = 'foo';

});

您还可以使用 .get()以获得类似的效果。

//retrieves an array of native DOM nodes using jQuery
var allDivNodes = $('div').get();

//retrieves the first node retrieved by the selector
var firstDiv = $('div').get(0);

//this is the same as get(0), however, using an array accessor can throw an exception
var firstDiv = $('div')[0];

A jQuery object normally contains a collection/array of DOM nodes. For instance, if you are iterating over a jQuery object such as -

$('div').each(function() {

  //In this context, this refers to the DOM node
  //and $(this) refers to a jQuery object for that particular DOM node
  //therefore $(this)[0] == this

   this.innerHTML = 'foo';
   $(this).html('foo');
   $(this)[0].innerHTML = 'foo';

});

You can also use .get() for a similar effect.

//retrieves an array of native DOM nodes using jQuery
var allDivNodes = $('div').get();

//retrieves the first node retrieved by the selector
var firstDiv = $('div').get(0);

//this is the same as get(0), however, using an array accessor can throw an exception
var firstDiv = $('div')[0];
再见回来 2024-11-10 18:52:09

所有 jQuery 查询都会返回所有匹配对象的列表。 :first 不保证单个结果,因此 [0] 获取单个元素。

All jQuery queries return a list of all objects that matched. :first does not guarantee a single result, thus [0] grabs a single element.

情话已封尘 2024-11-10 18:52:09
var $el = $cont.children(':first')

如果选择器匹配,则会为您提供一个带有匹配元素的类似数组的对象。您想要的是匹配的元素 - 这就是您使用[0]的原因 - 获取返回的第一个(也是唯一)元素“大批”。

这基本上就像 [element]element 之间的区别(其中,[element][0] = element

var $el = $cont.children(':first')

If the selector matches, that gives you an array-like object with the matched element. What you want is the matched element - that's why you use the [0] - to get the first (and only) element of the returned "array".

This is basically like the difference between: [element] and element (Where, [element][0] = element)

兲鉂ぱ嘚淚 2024-11-10 18:52:09

[0] 与使用 get(0) 获取 DOM 元素而不是 jQuery 是一样的元素。

[0] is the same thing as using get(0) to obtain the DOM element, not the jQuery element.

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