How to remove all li elements inside an ul except the first and the last elements using jQuery?

前端 未结 5 1489
小鲜肉
小鲜肉 2020-12-23 13:27

I have a

    element. How can remove all the
  • elements inside it, except for the first and the last, using jQuery?

5条回答
  •  没有蜡笔的小新
    2020-12-23 14:23

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

    This will remove all items from list Except/Exclude first and last element,


    To remove all items from any list

    $('#yourulid li').remove();  //remove all li items from list named/id #yourulid
    

提交回复
热议问题