如何获取parent 元素与document 相关的索引值,点击child,$(this )
, 元素?
HTML
<html>
<body>
<header>
<div class="gallery">
<div class="controls">
<button class="next"></button>
<button class="previous"></button>
</div>
</div>
</header>
<section>
<section>
<article>
<div class="gallery">
<div class="controls">
<button class="next"></button>
<button class="previous"></button>
</div>
</div>
<div class="gallery">
<div class="controls">
<button class="next"></button>
<button class="previous"></button>
</div>
</div>
</article>
</section>
</section>
</body>
</html>
在此 HTML 中,可以在正文中您喜欢的任何位置声明画廊。所以我需要一个“智能”选择器来解决这个问题。
伪JavaScript
$('.controls button').click(function() {
var hope = $(this).parents('.gallery').index();
alert(hope);
}
预期输出
场景
点击这个文档中second库的一个按钮:
1
最佳答案
试试这个:
$('.gallery').index( $(this).parents('.gallery') );
.index()
正在查找传递的元素在它所应用的元素组中的索引。
看看我的working jsFiddle example .
来源
关于javascript - 获取 child 的 parent 的 index() 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16059534/