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相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery操作json数据技巧汇总》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》及《jquery选择器用法总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyxpys.html