如何删除ul中的所有li元素,除了使用jQuery的第一个和最后一个元素? [英] How to remove all li elements inside an ul except the first and the last elements using jQuery?

查看:333
本文介绍了如何删除ul中的所有li元素,除了使用jQuery的第一个和最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

<$ p $(p> $('ul li:not(:first):n​​ot(:last)')。remove();

或使用特定的ID:



<$ p $('#code> $('#yourul li:not(:first):n​​ot(:last)')。remove();

如果你有一个带ul的jQuery对象:

  $(yourul).find('li:not(:first):n​​ot(:last)')。remove(); 


I have a <ul> element. How can remove all the <li> elements inside it, except for the first and the last, using jQuery?

解决方案

To remove from all ul-s on the page the li-s that are neither the first nor the last:

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

Or using a specific id:

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

If you have a jQuery object with your ul in it:

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

这篇关于如何删除ul中的所有li元素,除了使用jQuery的第一个和最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆