为什么Javascript中的removeChild总是删除最后一个孩子而不是从中获得id的孩子?

发布于 2024-11-29 15:56:09 字数 979 浏览 3 评论 0原文

我有四个带有事件侦听器 onclick 的 div, 调用一个 js 函数,它只执行以下操作:

this.parentNode.removeChild(this);

我希望它删除我单击的 div,但它没有。 相反,它会删除最后一个子项并将后面给出的 id 更改为 已删除子项的 id(第一次单击,最后一个子项),然后进一步单击 其他 div 将给定的 id 倒数到 1。去除 数组中从最后一个到第一个的子节点。

我尝试了很多变体,例如

document.getElementById('parentElementName').removeChild(this.gettAttribute('id'));

or

parent =document.getElementById('parentElementName');

to_be_removed = document.getElementById(this.gettAttribute('id');

parent.removeChild(to_be_removed);

或 childNodes // id = 1,2,3,4

to_be_removed =document.getElementById('box_content').childNodes[this.getAttribute('id')];

parent =document.getElementById('box_content');
    parent.removeChild(to_be_removed);

奇怪我可以成功更改可见性或背景颜色:

document.getElementById('box_content').childNodes[this.getAttribute('id')].style.visibility='hidden';

i have four divs with the eventlistener onclick,
calling a js function which just does the following :

this.parentNode.removeChild(this);

i expect it to remove the div i clicked on, but it does not.
instead it deletes the last child and changes the id given after to the
id of the removed child (first click, the last child) and by further clicking on the
other divs counts down the given id to one. removing
the childNodes in the array from the last to the first.

i tried a lot of variants, for example

document.getElementById('parentElementName').removeChild(this.gettAttribute('id'));

or

parent =document.getElementById('parentElementName');

to_be_removed = document.getElementById(this.gettAttribute('id');

parent.removeChild(to_be_removed);

or with childNodes // id = 1,2,3,4

to_be_removed =document.getElementById('box_content').childNodes[this.getAttribute('id')];

parent =document.getElementById('box_content');
    parent.removeChild(to_be_removed);

strange i can successfully change the visibility or the backgroundColor:

document.getElementById('box_content').childNodes[this.getAttribute('id')].style.visibility='hidden';

or

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2024-12-06 15:56:09

设法重制您想要的内容,请访问 http://jsfiddle.net/6YHcv/ 来查看。这是你所需要的吗?

如果您在 IE 上使用 attachEvent,则事件处理程序中的 this 可能会引用全局对象,而不是您的元素。否则我无法告诉你为什么你的代码不起作用。

Managed to remake what you intended, go to http://jsfiddle.net/6YHcv/ to check it out. Is this what you needed?

If you are on IE and use attachEvent, this in the event handler would probably refer to the global object, not your element. Otherwise I can't tell why your code isn't working.

清欢 2024-12-06 15:56:09

我在您的代码中看到一些拼写错误。我认为你的第二个例子应该工作得很好(见评论):

// make sure this is the parent element's ID and not the name, as this suggests.
    parent = document.getElementById('parentElementName'); 

//note the double 't' in getAttribute; also, you are missing an end bracket
    to_be_removed = document.getElementById(this.gettAttribute('id'); 

//looks good
    parent.removeChild(to_be_removed);

不过也看看这个例子: jsfiddle

它应该像在 onclick 处理程序之后调用函数 this.parentNode.removeChild(this) 一样简单。

I see a few typos in your code. Your second example should work just fine, I think (see comments):

// make sure this is the parent element's ID and not the name, as this suggests.
    parent = document.getElementById('parentElementName'); 

//note the double 't' in getAttribute; also, you are missing an end bracket
    to_be_removed = document.getElementById(this.gettAttribute('id'); 

//looks good
    parent.removeChild(to_be_removed);

Check out this example too, though: jsfiddle

It should be as simple as calling the function this.parentNode.removeChild(this) after an onclick handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文