Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a few divs and inside them I have nested divs. I would like to be able to get the index of the parent div my child divs reside in. Ie: If I clicked "post5" I would get the index1 or If I clicked "post4" it would still get index 1.

Html:

 <div class="more-content">
   <div class="post">post 1</div>
   <div class="post">post 2</div>
   <div class="post">post 3</div>
 </div>
<div class="more-content">
  <div class="post">post 4</div>
  <div class="post">post 5</div>
  <div class="post">post 6</div>
</div>
 <div class="more-content">
  <div class="post">post 7</div>
  <div class="post">post 8</div>
  <div class="post">post 9</div>
 </div>

Jquery:

$(".post").click(function() {
    alert($(".post").parent.index(this));
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

The following should work:

$(".post").click(function() {
    var index = $(this).parent().index(".more-content");
    alert(index);
});

DEMO: http://jsfiddle.net/NDySY/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...