Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择组的第i个jQuery对象比$($(.someclass)[i])更简单的方法?_Javascript_Jquery - Fatal编程技术网

Javascript 选择组的第i个jQuery对象比$($(.someclass)[i])更简单的方法?

Javascript 选择组的第i个jQuery对象比$($(.someclass)[i])更简单的方法?,javascript,jquery,Javascript,Jquery,因此,我尝试遍历“someclass”的成员,不是DOM元素,而是它们的jQuery对应项。我一直在使用$($(“.someclass”)[I]),但这很难看。有更自然的方法吗?用于迭代元素: $(".someclass").each(function() { // use $(this) }); 您可以使用以下方法: 这就是说,Felix Kling是对的:最好使用循环而不是循环来迭代jQuery对象中存储的元素。可以使用jQuery $("ul li:nth-child(1)").

因此,我尝试遍历“someclass”的成员,不是DOM元素,而是它们的jQuery对应项。我一直在使用$($(“.someclass”)[I]),但这很难看。有更自然的方法吗?

用于迭代元素:

$(".someclass").each(function() {
    // use $(this)
});
您可以使用以下方法:

这就是说,Felix Kling是对的:最好使用循环而不是循环来迭代jQuery对象中存储的元素。

可以使用jQuery

$("ul li:nth-child(1)").addClass("green"); //this select 1 the element  of group

您可以使用
each
方法迭代元素:

$(".someclass").each(function(i, e){
  // i is the index
  // e is the element
  // this is the element
  // $(this) is the element wrapped in a jQuery object
  $(this).append("<span>" + (i+1) + "</span>");
});
$(“.someclass”)。每个(函数(即){
//我是索引
//e是元素
//这就是元素
//$(this)是包装在jQuery对象中的元素
$(this.append(“+(i+1)+”);
});
(回调函数中的参数通常是可选的。)

一些用于操作元素的方法可以采用回调方法,以便您可以使用该方法来迭代元素。例如:

$(".someclass").append(function(i, h){
  // i is the index
  // h is the current html content
  // Return html to append:
  return "<span>" + (i+1) + "</span>";
});
$(.someclass”).append(函数(i,h){
//我是索引
//h是当前的html内容
//返回要追加的html:
返回“+(i+1)+”;
});
试试:

$(".someclass:eq("+yourNumberHere+")")
$(".someclass:eq("+yourNumberHere+")")