Tom*_*kay 3 javascript indexing jquery gallery selector
我如何得到父母的关系元素的索引值的文件,点击的的孩子,$(this),元素?
<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>
Run Code Online (Sandbox Code Playgroud)
在此HTML中,可以在正文中的任何您喜欢的位置声明图库.所以我需要一个"智能"选择器来解决问题.
$('.controls button').click(function() {
var hope = $(this).parents('.gallery').index();
alert(hope);
}
Run Code Online (Sandbox Code Playgroud)
场景
单击此文档中第二个库的按钮:
1
Run Code Online (Sandbox Code Playgroud)
试试这个:
$('.gallery').index( $(this).parents('.gallery') );
Run Code Online (Sandbox Code Playgroud)
.index() 正在查找应用它的元素组中传递元素的索引.
看一下我工作的jsFiddle示例.
源极(S)