Man*_*gir 2 jquery dom nextsibling
我有以下DOM结构:
<label>
<span class="tt">
<a href="#" class="editable">Hello1</a>
<a href="#" class="editable">Hello2</a>
<a href="#" class="editable">Hello3</a>
<a href="#" class="editable">Hello4</a>
</span>
<a href="#" class="editable">Hello5</a>
</label>
Run Code Online (Sandbox Code Playgroud)
当我点击锚点时Hello3,我需要得到Hello4它的工作绝对正常.但如果我点击链接Hello4,我需要得到Hello5哪些我没有得到正确的.
我正在使用以下代码:
$(document).on('click','.editable',function(){
console.log($(this).nextAll('.editable').first());
});
Run Code Online (Sandbox Code Playgroud)
我真正想要的是editable当我点击一个锚时,让下一个元素在label标签中有类.
您不能使用nextAll(),因为它基于兄弟元素,在您的情况下,所有editable元素都不是兄弟.
您可以使用基于索引的查找来查找下一个元素
$(document).on('click', '.editable', function() {
var $all = $('.editable');
snippet.log($all.eq($all.index(this) + 1).text());
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<label>
<span class="tt">
<a href="#" class="editable">Hello1</a>
<a href="#" class="editable">Hello2</a>
<a href="#" class="editable">Hello3</a>
<a href="#" class="editable">Hello4</a>
</span>
<a href="#" class="editable">Hello5</a>
</label>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |