jQuery JQuery获取数组的第n个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10568990/
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-26 09:32:19  来源:igfitidea点击:

JQuery get the nth element of array

jqueryarrays

提问by Norse

$('#button').click(function() {
   alert($('.column').val());
});

How could I get the first, second, or third element in .column?

我怎样才能获得第一个、第二个或第三个元素.column

回答by Elliot Bonneville

$('#button').click(function() {
    alert($('.column').eq(0).val()); // first element
    alert($('.column').eq(1).val()); // second
    alert($('.column').eq(2).val()); // third
});

回答by Blender

I like selector strings, so I usually do this:

我喜欢选择器字符串,所以我通常这样做:

$('.column:eq(3)') // Fourth .column element

回答by Jeff

Use the Slice() function: http://api.jquery.com/slice/

使用 Slice() 函数:http: //api.jquery.com/slice/

See question: How to select a range of elements in jQuery

请参阅问题: 如何在 jQuery 中选择一系列元素

$('.column').slice(0, 2).each(function() {
    $(this).val();  /* my value */
});

回答by Sampson

You could use the :lt(less-than) selector and tell it you want 0, 1, and 2by indicating all .columnelements below index 3:

您可以使用:lt(小于)选择器并0, 1, and 2通过指示.columnindex 下的所有元素来告诉它您想要3

$("#button").on("click", function(){
  $(".column:lt(3)").each(function(){
    alert( this.value );
  });
});

Demo: http://jsbin.com/icevih/edit#javascript,html

演示:http: //jsbin.com/icevih/edit#javascript,html