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  >  Article  >  Web Front-end  >  How to dynamically delete LI using jQuery

How to dynamically delete LI using jQuery

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 10:17:552988browse

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)].

How to dynamically delete LI using jQuery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn