gpt4 book ai didi

javascript - 如何获取选择数组中元素的索引?

转载 作者:行者123 更新时间:2023-12-02 23:39:18 25 4
gpt4 key购买 nike

我需要获取与其父元素相关的多个元素的索引。在下面的代码中,我认为它应该为我提供相同的注释和标签索引,因为它们在相同的<ol>中但这没有发生。

$('#main-form').find('form').each(function () { //foreach form in the ol tag
var $drop_target_index = $('.drop_targets').index($(this).closest('.drop_targets'));
console.log($(this).closest('.drop_targets'));
console.log( $drop_target_index);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main-form">
<ol class="drop_targets">
<li><ol class="drop_targets comments">
<li><form></form>

Item 1 </li>
</ol>
<ol class="drop_targets tags">
<li><form></form>
Item 2</li>
</ol></li>
</ol>
</div>

我预计00我有01

最佳答案

each().drop_targets 一起使用。

$('.drop_targets .drop_targets').each(function () {
var $drop_target_index = $(this).parent().index() + 1;
console.log($drop_target_index);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main-form">
<ol class="drop_targets">
<ol class="drop_targets comments">
<form></form>
<form></form>
<form></form>

Item 1
</ol>
<ol class="drop_targets tags">
<form></form>
<form></form>
<form></form>
Item 2
</ol>
</ol>
</div>

关于javascript - 如何获取选择数组中元素的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com