检查子容器的父容器 javascript
只是想知道如何检查元素(即 div 容器)中的元素(即按钮)以及它们是否存在,将其删除?
如果我将一个子项附加到 div,我如何在下次查看时检查该子项或那些类型的子项?即添加;
example = document.getElementById('div');
example.appendChild(aButton);
//loop to look for aButton / button type in example div
干杯
Just wondering how you can check an element (i.e. a div container) for elements (i.e. buttons) and if they exist, remove them?
If I append a child to a div, how can I on the next look check for that child or those type of children? i.e. adding;
example = document.getElementById('div');
example.appendChild(aButton);
//loop to look for aButton / button type in example div
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取 childNodes 数组,循环遍历并查找与您的条件匹配的一个(带有类型按钮的输入标记,或者可能是按钮标记)
示例: http://jsfiddle.net/BZMbk/3/
Get the childNodes array, loop through and look for one matching your criteria ( input tag with type button, or possibly a button tag)
Example: http://jsfiddle.net/BZMbk/3/
要从文档的子集中获取节点类型的元素,您可以简单地执行此操作,
这将返回 id 为“div”的元素下的任何按钮(顺便说一句,这对于 id 来说不是一个好名称)
To get elements that are of a node type from a subset of the document you can simply do
This will return any buttons under an element with the id of "div" (not a good name for an id btw)
您可以使用
children
然后循环遍历子项。例如,如果您有一个具有以下设置的 div:然后您可以让子级循环通过它们,如下所示:
至于删除它们,就像这样简单
You could use
children
and then loop through the children. For example if you have a div with the following set up:You could then get the children an loop through them like this:
And as to remove them it would be as simple as