比$($(".someclass")[i]))更简单的选择jQuery对象组的方法
Easier way to select ith jQuery object of group than $($(".someclass")[i]))?
所以我试图通过"someeclass"的成员循环,而不是DOM元素,但他们的jQuery对应物。我一直在使用$($(". someeclass ")[I]),但这很难看。有没有更自然的方法?
您可以使用eq()方法:
$(".someclass").eq(i);
也就是说,Felix Kling是正确的:您最好使用each()而不是循环来遍历存储在jQuery对象中的元素。
使用.each()
[docs]来遍历元素:
$(".someclass").each(function() {
// use $(this)
});
可以使用jquery的n -child
$("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").append(function(i, h){
// i is the index
// h is the current html content
// Return html to append:
return "<span>" + (i+1) + "</span>";
});
尝试:
$(".someclass:eq("+yourNumberHere+")")
相关文章:
- 使用javascript"这个“;用于jquery选择器
- 我如何选择/点击一个带有for=“”的元素"属性[量角器]
- Bootstrap日期选择器键盘导航:选择日期时"突出显示”;
- JQuery:如何强制选择一个选项为"选择“;,使用FancySelect插件
- jQuery"不包含“;选择器
- jQuery"OR”;具有共享修饰符的选择器
- 用“<选择></选择>"类型
- “间歇”;jqLite不支持通过选择器查找元素"当使用RequireJS加载AngularJS时
- "更改“;添加要选择的选项时正在激发
- "粘性“;在Angular应用程序中选择
- Jqgrid编辑类型:"选择“;事件
- "ReferenceError:$未定义“;尝试使用日期时间选择器时
- Javascript/HTML”;错误”:"请使用POST请求“;将下拉选择与case/switch语句一起使
- Knockout.js动态选择模板错误:"找不到ID为'的模板'&”;
- 使用<重置所有字段(复选框、文本、选择);body onload()">
- 获取"data something“;从<选项>包含在<选择>
- 选择A之后的所有文本”#"带有正则表达式的符号
- 我的onClick事件没有设置“;selectedValue"用户选择的选项中的变量
- "选择“;使用jquery两次
- "交叉选择“;在Dojo中