jQuery 如何获得 eq() 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4849677/
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
How to get the eq() value?
提问by Ryan
Is this possible? For me to get the eq()
value? For example, if I click the li:eq(2)
, var x
will become 2
. Here's the code.
这可能吗?让我得到eq()
价值?例如,如果我单击li:eq(2)
,varx
将变为2
。这是代码。
$('#numbers ul li').click(function(){
x=$(this).eq().val();
alert(x);
});
回答by jAndy
The .index()
what is this?method will do it.
这.index()
是什么?方法会做到的。
$('#numbers ul li').click(function() {
var self = $(this),
index = self.index(),
text = self.text();
alert(text + ' ' + index);
});
回答by Swanidhi
The answer above is wrong. Index provides a relative value with respect to its siblings. Hence, the value is expected to change.
上面的答案是错误的。索引提供与其兄弟姐妹相关的相对值。因此,该值预计会发生变化。
It should be something like
它应该是这样的
$('.someClass').click(function(){
var that_ = this;
// your logic for this function
....
....
var currentIndex = $('.someClass').index(_that);
});
回答by Müslüm CENG?Z
eq<>
index:
eq<>
指数:
scount=$(selector).length;
for(i=0; i<scount; i++){
$("selector:eq("+i+")").attr("eq",i);
}
$("selector").click(function(){
alert($(this).attr("eq"));
});
回答by cnz018
You must give a context to index()
in order to have an equivalent of eq()
. Otherwise index()
returns a relative value.
您必须提供一个上下文index()
,才能获得eq()
. 否则index()
返回一个相对值。
$('#numbers ul li').click(function(){
var x=$(this).index('#numbers ul li');
alert(x);
});
回答by Prabhagaran
Try this one..
试试这个..
$('#numbers ul li').click(function(){
var x=$(this).index();
alert(x);
});