jquery - 如何使用 jQuery 删除 ul 中除第一个和最后一个元素之外的所有 li 元素?

标签 jquery html html-lists

我有一个 <ul>元素。如何删除所有 <li>其中的元素,除了第一个和最后一个,使用 jQuery?

最佳答案

要从页面上的所有 ul-s 中删除既不是第一个也不是最后一个的 li-s:

$('ul li:not(:first-child):not(:last-child)').remove();

或者使用特定的id:

$('#yourul li:not(:first-child):not(:last-child)').remove();

如果您有一个包含 ul 的 jQuery 对象:

$(yourul).find('li:not(:first-child):not(:last-child)').remove();

关于jquery - 如何使用 jQuery 删除 ul 中除第一个和最后一个元素之外的所有 li 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/906432/

相关文章:

javascript - 更改 getElementsByClassName 的样式会导致编译错误

javascript - Uncaught ReferenceError : $ is not defined in HTML

javascript - 光滑的 slider 定位和宽度

php - 如何将数据发布为数组的索引数组(不指定索引)

javascript - 如何忽略先前的选择并只写入最后选择的内容

html - Twitter Bootstrap 选择标签问题

CSS - 菜单上的统一响应式背景图像

html - 列表元素在删除后保留为空白

html - CSS MDL : vertically align all list elements results

javascript - 跨多个 DIV 的随机闪烁速度