简体   繁体   English

在具有id的div内的ul里面的li上的removeChild?

[英]removeChild on a li inside a ul inside a div with an id?

I have the following code on a webpage: 我在网页上有以下代码:

<div id="emails">
<ul>
<li>email1</li>
<li>email2</li>
</ul>
</div>

<a href="#" onClick="deleteEmail()">click</a>

and I want to remove the first email, i've been trying with this: 我想删除第一封电子邮件,我一直在尝试这个:

function deleteEmail(){
    var ul = document.getElementById('emails').getElementsByTagName('ul');
    ul.removeChild(ul.childNodes[0]);
}

im kinda new to Javascript DOM, so if there's a better way to do it, please let me know. 我是Javascript DOM的新手,所以如果有更好的方法,请告诉我。

Note: I would like to do this without any kind of js framework. 注意:我想在没有任何js框架的情况下这样做。

Most correct: 最正确的:

var ul = document.getElementById('emails').getElementsByTagName('ul')[0];
var li = ul.getElementsByTagName('li')[0];
ul.removeChild(li)

1) More correct string: 1)更正确的字符串:

var ul = document.getElementById('emails').getElementsByTagName('ul')[0];

2) Note that in "ul.childNodes[i]" i will be 0 for spaces, 1 for <li>email1</li> , 2 for space etc. You should use ul.getElementsByTagName('li')[i] if you're insterested only in <li> s. 2)请注意,在“ul.childNodes [i]”中,我将为0表示空格,1表示<li>email1</li> ,2表示空格等。您应该使用ul.getElementsByTagName('li')[i]如果你只对<li> s感兴趣

The children of a DOM node include text and comments, not just the elements, so in order to figure out what index the element you want to remove is at, you need to take them into account. DOM节点的子节点包括文本和注释,而不仅仅是元素,因此为了确定要删除的元素所在的索引,您需要考虑它们。 In your case, the index of the first <li> in the <ul> is 1 . 在您的情况下, <ul>第一个<li>的索引是1

The DOM for your `email' div looks like: 您的`email'db的DOM如下所示:

DIV
  text( whitespace )
  UL
    text ( whitespace )
    LI 
      text (email1)
    text ( whitespace )
    LI
      text (email2)
    text ( whitespace )
  text (whitespace)

That being said, it's probably easiest to directly find the <li> you want to remove and then remove it. 话虽如此,最简单的方法是直接找到要删除的<li> ,然后将其删除。

var toRemove = document.
      getElementById('emails').
      getElementsByTagName('ul')[0]. 
      getElementsByTagName('li')[0];

toRemove.parentNode.removeChild( toRemove );
<div id="emails">
<ul>
<li>email1</li>
<li>email2</li>
</ul>
</div>

<a href="#" onClick="deleteEmail()">click</a> 
<!--note that I made it so you actually invoke deleteEmail -->

and I want to remove the first email, i've been trying with this: 我想删除第一封电子邮件,我一直在尝试这个:

function deleteEmail(){
    //I believe you meant emails =/
    var ul = document.getElementById('emails').getElementsByTagName('ul');
    ul.removeChild(ul.getElementsByTagName("li")[0]);
}

If you turn the string "email#" into a link... something like this: 如果你把字符串“email#”变成一个链接......就像这样:

<a href="" onClick="removeElement(this.parentNode);return false">email1</a>

With a function similar to this. 具有与此类似的功能。

function removeElement(childElm) {
  var parentElm = childElm.parentNode;
  parentElm.removeChild(childElm);
}

Though you can use removeElement() without the onClick but I just like the simplicity it allows. 虽然你可以使用removeElement()而不使用onClick,但我只是喜欢它允许的简单性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM