Javascript 如何通过它的 id 删除我的 ul 中的所有 li?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5801721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:56:24  来源:igfitidea点击:

how to remove all li in my ul through with it id?

javascriptjquerylist

提问by w3father

I have a ul list.

我有一个 ul 列表。

I want to remove all li in my ul through with it id?

我想通过它的 id 删除我的 ul 中的所有 li?

回答by RobG

All that jQuery stuff. :-)

所有 jQuery 的东西。:-)

A plain old javascript version:

一个普通的旧 javascript 版本:

  var ul = document.getElementById('<ul id>');
  if (ul) {
    while (ul.firstChild) {
      ul.removeChild(ul.firstChild));
    }
  }

You could also set:

您还可以设置:

  ul.innerHTML = '';

or even:

甚至:

  ul.parentNode.replaceChild(ul.cloneNode(false), ul);

but I prefer the first method.

但我更喜欢第一种方法。

回答by ThiefMaster

If you want to remove all lielements inside a ulwith a certain ID:

如果要删除具有特定 ID 的lia中的所有元素ul

$('#id-of-ul > li').remove();

Since ulhas only lichildren anyway you can also use $('#id-of-ul').empty()to delete all its children.

由于无论如何ul只有li孩子,您也可以使用$('#id-of-ul').empty()删除其所有孩子。

回答by Sylvain

You could also use empty():

您还可以使用empty()

$('#listId').empty();

回答by Piotr Kula

*Using jQuery

*使用jQuery

Did you mean that you want to remove all li with the same id? (This is how I interpreted the question)

您的意思是要删除所有具有相同 id 的 li 吗?(这就是我解释问题的方式)

$('#id').remove()

(Remember that using the same ID on multiple DOM elements is not considered good programming and should always be avoided! Always use a good IDE to chceck your code)

(请记住,在多个 DOM 元素上使用相同的 ID 不被认为是好的编程,应该始终避免!始终使用好的 IDE 来检查您的代码)

You can also remove using the class (this is a better way to remove grouped elements by class name)

您还可以使用类删除(这是按类名删除分组元素的更好方法)

$('.className').remove()

Those two functions will remove ALL elements with the ID / Class To restrict removing id's / class'es from LI only

这两个函数将删除所有带有 ID / Class 的元素以限制仅从 LI 中删除 id/class'es

$('li #id').remove();
$('li .className').remove();

You can also make a function in JavaScript. This will remove any DOM with a class itemDelete clicked on it.

您还可以在 JavaScript 中创建一个函数。这将删除任何点击了 itemDelete 类的 DOM。

$('.itemDelete').live("click", function() {
        var id = $(this).parent().get(0).id;
        $("#" + id).remove();
    });

Make a loop and call a function?

做一个循环并调用一个函数?

function deleteLI(id) {
            $("#" + id).remove();
        };

回答by Richard H

The quick dirty, pure js way of doing this would be:

这样做的快速肮脏,纯 js 方法是:

var ul = document.getElementById("ul_id");
ul.innerHTML = "";

This of course assumes there are not other non-li children that you don't want to clobber.

这当然假设没有其他您不想破坏的非礼儿童。

回答by Kevin

You'll have to make a call to the remove()method.

您必须调用该remove()方法。

// Removes a single element with the specified id
$('#id-of-the-element').remove();

Have a look at the documentation for some more information.

查看文档以获取更多信息。

回答by Sumit

The following should work:
$('#uiId').remove('li');

以下应该工作:
$('#uiId').remove('li');