Tan*_*moy 45 html jquery html-lists
我有一个<ul>元素.<li>除了第一个和最后一个,如何使用jQuery 删除其中的所有元素?
Mar*_*ger 107
要从页面上的所有ul-s中删除既不是第一个也不是最后一个的li-s:
$('ul li:not(:first-child):not(:last-child)').remove();
Run Code Online (Sandbox Code Playgroud)
或者使用特定的ID:
$('#yourul li:not(:first-child):not(:last-child)').remove();
Run Code Online (Sandbox Code Playgroud)
如果你有一个带有ul的jQuery对象:
$(yourul).find('li:not(:first-child):not(:last-child)').remove();
Run Code Online (Sandbox Code Playgroud)
Sam*_*war 18
您是否尝试过选择所有列表元素,然后从列表中删除第一个和最后一个?
$('ul li').not('li:first, li:last').remove()
Run Code Online (Sandbox Code Playgroud)
小智 7
$('ul li').not('li:first, li:last').remove();
Run Code Online (Sandbox Code Playgroud)
这将删除列表中的所有项目除外/排除第一个和最后一个元素,
从任何列表中删除所有项目
$('#yourulid li').remove(); //remove all li items from list named/id #yourulid
Run Code Online (Sandbox Code Playgroud)