Run index() into only specific html tag
I want index() runs into only specific html tag .row
But, right now index() runs all the DOM, and give me all the .twocol in the document.
I have the full example here: http://jsfiddle.net/brunogarcia/SRUkt/ Why?
$("a.mostrar").live("click", function(event) {
alert($(this).parent(".row .twocol").index(".row .twocol"));
<div class="row">
<h3>Row 1开发者_StackOverflow社区</h3>
<div class="twocol"><a href="" class="mostrar">text</a></div>
<div class="twocol"><a href="" class="mostrar">text</a></div>
</div>
<div class="row">
<h3>Row 2</h3>
<div class="twocol"><a href="" class="mostrar">text</a></div>
<div class="twocol"><a href="" class="mostrar">text</a></div>
</div>
I think what you want is this:
$(this).parent().index();
It will give you the 1-based index of the link's parent among its siblings.
See your updated fiddle.
If this is not what you want, you have to explain better.
If you know the index, you can use
$(this).parent(".row .twocol").index(".row .twocol:eq(2)")
it will get the 3rd one.
精彩评论