有没有办法通过 JQuery [$('.a').index(1).remove();] 中的索引值删除元素?



我想使用jQuery删除其索引一组HTML元素。有什么办法可以做吗?

我尝试了类似于$('。a'(的一些东西。索引(1(.remove((;但是它不起作用。

/* This is not the actual code, but sample code */
// HTML Part
<ul>
  <li class="a">Milk</li>
  <li class="a">Tea</li>
  <li class="a">Coffee</li>
</ul>
// jQuery Part
$('.a').index(1).remove();
/* If I want to remove Tea from list using its index value */

使用eq()代替index()

$('.a').eq(0).remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="a">Milk</li>
  <li class="a">Tea</li>
  <li class="a">Coffee</li>
</ul>

最新更新