JavaScript

超轻量级php框架startmvc

jQuery实现动态删除LI的方法

更新时间:2020-05-15 01:00:02 作者:startmvc
本文实例讲述了jQuery实现动态删除LI的方法。分享给大家供大家参考,具体如下:我们有时

本文实例讲述了jQuery实现动态删除LI的方法。分享给大家供大家参考,具体如下:

我们有时候知道UL的id,但是苦不堪言的找不到LI进行清除,这边有一些办法可以参考,设置Li的id是不错的方法,但是千万别设置成一个ID,到时候删除只会删除第一个

可以使用


$("#ul li").not(":first").remove();

这个方式可以删除不是第一个li的其他所有li.

我这边是用:


$(document).ready(function(){
 $("#search_content").keyup(function () {
 if(CheckChinese($("#search_content").val()))
 {
 $.ajax({
 type: "POST",
 anync: true,
 url: "HelpCenterSuggestion.ashx",
 cache: false,
 dataType: "text",
 data: { m: $("#search_content").val() },
 success: function (result) {
 alert(result);
 $("#UlContent li").remove();
 $("#UlContent").append(result);
 }
 });
 }
 });

方法很多,还可以用:

1、用not


$("ul>li").not(":eq(0)").remove();


$("ul>li").not(":first").remove();

2、用filter


$("ul>li").filter(function(index){
return index!=0;
}).remove();

jQuery 动态删除 LI