从ul删除li元素 [英] Removing li elements from ul

查看:89
本文介绍了从ul删除li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用JavaScript从ul中动态删除几个li元素,给定li元素的id?



更新实际问题: / p>

我有以下列表。

 < ul id = attributesdata-role =listview> 
< li id =attrib01> Attribute1< / li>
< li id =attrib02> Attribute2< / li>
< li id =attrib03> Attribute3< / li>
< li id =attrib04> Attribute4< / li>
< li id =attrib05> Attribute5< / li>
< / ul>

在ajax请求/响应之后,如果某个特定属性是undefined,我想将其删除从列表中。

  if(typeof data.attrib1 ===undefined)
$(#attrib01 )。去掉();

我确定我收到了正确的ajax响应。所以,现在的问题是,当我删除attrib4时,attrib [1-3]也被删除。任何想法为什么会发生这种情况?

解决方案

尝试

  var elem = document.getElementById('id'); 
elem.parentNode.removeChild(elem);


Is it possible using JavaScript to dynamically remove just a few li elements from a ul, given the id's of the li elements?

UPDATE about the actual issue:

I've the following list.

<ul id="attributes" data-role="listview">
    <li id="attrib01">Attribute1</li>
    <li id="attrib02">Attribute2</li>
    <li id="attrib03">Attribute3</li>
    <li id="attrib04">Attribute4</li>
    <li id="attrib05">Attribute5</li>
</ul>

After a ajax request/response, if a particular attribute is "undefined", I want to remove it from the list.

if(typeof data.attrib1 === "undefined")
    $("#attrib01").remove();

I've made sure I'm receiving the correct ajax response. So, the problem now is, that when I remove attrib4, attrib[1-3] are being removed as well. Any idea why this might be happening?

解决方案

Try

var elem = document.getElementById('id');
elem.parentNode.removeChild(elem);

这篇关于从ul删除li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆