本文介绍了全部删除< li>来自< ul>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码在 ul
中附加 li
:
I am appending li
in a ul
using the following code:
for (var i = 1; i <= len; i++) {
li = document.createElement('li');
element = document.createElement("img");
element.setAttribute("src", path[i]);
li.appendChild(element);
root.appendChild(li);
}
现在,我想在点击按钮时从列表中删除所有项目。这就是我正在使用的,它不起作用:
Now, I want to remove all items from the list on a button click. This is what I am using, which isn't working:
while(root.hasChildNodes()){
root.removeChild('li');
}
条件为真,但内线 root。 removeChild('li')
不起作用。我也试过这些选项:
The condition is true but the inner line root.removeChild('li')
doesn't work. I also tried these options:
root.removeChild(root li);
root.removeChild('#listid li');
root.removeChild('ul li');
...
推荐答案
如果您正在使用jQuery,为什么不使用它的好处?
If you are using jQuery, why don't you use it's benefits?
添加< li>
元素:
$("<li><img src='"+path[i]+"'></li>").appendTo(root);
删除所有< li>
元素:
$(root).empty();
删除一个< li>
元素:
$("li:eq(3)",$(root)).remove();
如果你使用原始js,你可以使用:
and if you are using raw js, you can use:
document.getElementById("root").innerHTML = "";
这篇关于全部删除< li>来自< ul>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!