jQuery 中的元素数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7622704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:04:17  来源:igfitidea点击:

An array of elements in jQuery

jqueryarrays

提问by SirRoland

Markup code:

标记代码:

<div id="elements">
    <div>
        <a href="#">text</a>
        <a href="#">text</a>
        <a href="#">text</a>
    </div>
    <div>
        <a href="#">text</a>
        <a href="#">text</a>
        <a href="#">text</a>
    </div>
    <div>
        <a href="#">text</a>
        <a href="#">text</a>
    </div>
</div>

Please tell me how can I get an array of all elements of the div, so that later, it is possible to address an array of options?

请告诉我如何获得 的所有元素的数组div,以便以后可以寻址选项数组?

Such as:

如:

divs[0]  
links[1] 

回答by meder omuraliev

$('#elements div').eq(0) // first div

$('#elements div a').eq(1) // second anchor in group of divs

Without the .eq()you have a collection of elements, the .eqjust singles the elements out.

没有.eq()你有一个元素的集合,.eq只是把元素挑出来。

回答by Shef

Demo

演示

var divs = $('#elements div');
var links = $('#elements div a');
  1. If you want the DOM elements, then you can access them with the array style like divs[0]or links[2].
  2. If you want to get the specific jQuery object, you can access them with divs.eq(0)or links.eq(1).
  1. 如果您想要 DOM 元素,那么您可以使用数组样式(如divs[0]或 )访问它们links[2]
  2. 如果要获取特定的 jQuery 对象,可以使用divs.eq(0)或访问它们links.eq(1)

回答by Paul Schwarz

See https://api.jquery.com/toArray/

https://api.jquery.com/toArray/

$( "li" ).toArray()

This works great in cases like

这在以下情况下非常有效

$( "li" ).toArray().each( function(li) {
  console.log('do something with this list item', li);
})

or if you really want

或者如果你真的想要

$( "li" ).toArray()[0]

回答by mowwwalker

wrapper = document.getElementById('elements');
divs = wrapper.getElementsByTagName('div');
links = [];
for (i = 0; i < divs.length; i++) {
    links[i] = divs[i].getElementsByTagName('a');
}

divs will now be an array of the divs inside of your "elements" div. Links is now a two dimensional array, where the first index refers to the div inside your "elements" div, and the second index refers to the "a" tags inside that div.

divs 现在将是“元素”div 内的 div 数组。Links 现在是一个二维数组,其中第一个索引指的是“元素”div 中的 div,第二个索引指的是该 div 中的“a”标签。

links[2][1]

links[2][1]

will refer to the element denoted below:

将参考下面表示的元素:

<div id="elements">
    <div>
        <a href="#">text</a>
        <a href="#">text</a>
        <a href="#">text</a>
    </div>
    <div>
        <a href="#">text</a>
        <a href="#">text</a>
        <a href="#">text</a>
    </div>
    <div>
        <a href="#">text</a>
        <a href="#">text</a>  //this one
    </div>
</div>

回答by maxijb

Or also, and I believe this is more accurately what you asked for:

或者,我相信这更准确地是您要求的:

$("#elements div:eq(0) a:eq(1)")

$("#elements div:eq(0) a:eq(1)")

回答by Mirko

$("#elements a")

$("div a")

or $("a")

jQuery Selectors

jQuery 选择器