开发者

How to get the eq() value?

开发者 https://www.devze.com 2023-02-07 11:14 出处:网络
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.

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.

$('#numbers ul li').开发者_运维技巧click(function(){
  x=$(this).eq().val();
  alert(x);
});


The .index()what is this? method will do it.

$('#numbers ul li').click(function() {
  var self   = $(this),
      index  = self.index(),
      text   = self.text();

  alert(text + ' ' + index);
});

Demo: http://www.jsfiddle.net/Y2aDP/


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);
});


eq<> index:

scount=$(selector).length;
for(i=0; i<scount; i++){
    $("selector:eq("+i+")").attr("eq",i);
}
$("selector").click(function(){
    alert($(this).attr("eq"));
});


Try this one..

 $('#numbers ul li').click(function(){
   var x=$(this).index();
   alert(x);
 });


You must give a context to index() in order to have an equivalent of eq(). Otherwise index() returns a relative value.

 $('#numbers ul li').click(function(){
   var x=$(this).index('#numbers ul li');
   alert(x);
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消