我有一个 <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/