li").not(":eq(0)").remove()]; 2. Use filter to judge, the code is 【$("ul>li").filter(function(index)】."/> li").not(":eq(0)").remove()]; 2. Use filter to judge, the code is 【$("ul>li").filter(function(index)】.">
Home >Web Front-end >JS Tutorial >How to dynamically delete LI using jQuery
JQuery's method of dynamically deleting LI: 1. Use not to judge, the code is [$("ul>li").not(":eq(0)").remove()]; 2. Use filter to judge, the code is [$("ul>li").filter(function(index)].
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.
Recommendation: jquery video tutorial
jQuery method to dynamically delete LI:
Sometimes we know the ID of UL, but it is miserable to find the LI to clear it. Here are some methods for reference. Setting the ID of Li is a good method, but do not set it to an ID. When the time comes, Deletion will only delete the first
You can use
$("#ul li").not(":first").remove();
This method can delete all other li that are not the first li.
I use:
$(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); } }); } });
There are many methods, you can also use:
1, use not
$("ul>li").not(":eq(0)").remove();
or
$("ul>li").not(":first").remove();
2 , use filter
$("ul>li").filter(function(index){ return index!=0; }).remove();
Related free learning recommendations:javascript(Video)
The above is the detailed content of How to dynamically delete LI using jQuery. For more information, please follow other related articles on the PHP Chinese website!