扫码关注官方订阅号
<ul> <li><li/> <li><li/> <li><li/> <li><li/> <ul>
上面代码,我想保留第一个li,其他的li删除,请问有什么方法吗,谢谢!
认证高级PHP讲师
$("ul li").eq(0).siblings().remove();
eq() 方法返回被选元素中带有指定索引号的元素;siblings() 方法返回被选元素的所有同胞元素。你可以去看看jQuery教程http://www.runoob.com/jquery/jquery-tutorial.html。
//原生的js function remove(oparent,ochild,start,offset){ var parent = document.querySelector(oparent), // 获取父级元素 chilren = parent.querySelectorAll(ochild), // 获取子级元素 len = chilren.length,// 子元素的长度 start = start || 0, // 开始的位置 offset = offset ? start+offset : len; // 删除的数量,offset大于0,如果offset存在的话,那么开始位置加上位移,否则就是元素的长度剩余的长度; if(len<=start) return; for(var i = start; i< offset;i++ ){ parent.removeChild(chilren[i]); } }
$("ul > li:not(:first-child)").remove();
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
eq() 方法返回被选元素中带有指定索引号的元素;
siblings() 方法返回被选元素的所有同胞元素。
你可以去看看jQuery教程http://www.runoob.com/jquery/jquery-tutorial.html。