检查子容器的父容器 javascript

发布于 2024-11-30 15:20:43 字数 308 浏览 4 评论 0原文

只是想知道如何检查元素(即 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 技术交流群。

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

发布评论

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

评论(3

╰◇生如夏花灿烂 2024-12-07 15:20:43

获取 childNodes 数组,循环遍历并查找与您的条件匹配的一个(带有类型按钮的输入标记,或者可能是按钮标记)

var children = example.childNodes;

for(var i = 0; i<children.length; i++){
    if( children[i].tagName == "INPUT" && children[i].type=='button' ) {
        example.removeChild( children[i] );
        i--; //Decrement counter since we are removing an item from the list
    }
}

示例: 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)

var children = example.childNodes;

for(var i = 0; i<children.length; i++){
    if( children[i].tagName == "INPUT" && children[i].type=='button' ) {
        example.removeChild( children[i] );
        i--; //Decrement counter since we are removing an item from the list
    }
}

Example: http://jsfiddle.net/BZMbk/3/

世态炎凉 2024-12-07 15:20:43

要从文档的子集中获取节点类型的元素,您可以简单地执行此操作,

document.getElementById('div').getElementsByTagName("button")

这将返回 id 为“div”的元素下的任何按钮(顺便说一句,这对于 id 来说不是一个好名称)

To get elements that are of a node type from a subset of the document you can simply do

document.getElementById('div').getElementsByTagName("button")

This will return any buttons under an element with the id of "div" (not a good name for an id btw)

勿挽旧人 2024-12-07 15:20:43

您可以使用 children 然后循环遍历子项。例如,如果您有一个具有以下设置的 div:

<div id='div'>
    <input type='button'> 
    <p>here</p>
    <a href="#"></a>
</div>

然后您可以让子级循环通过它们,如下所示:

var example = document.getElementById("div");
var children = example.children;
alert(children.length);
for(var i = 0; i < children.length; i++)
{
    alert(children[i].tagName);
}

至于删除它们,就像这样简单

example.removeChild( children[i] );

You could use children and then loop through the children. For example if you have a div with the following set up:

<div id='div'>
    <input type='button'> 
    <p>here</p>
    <a href="#"></a>
</div>

You could then get the children an loop through them like this:

var example = document.getElementById("div");
var children = example.children;
alert(children.length);
for(var i = 0; i < children.length; i++)
{
    alert(children[i].tagName);
}

And as to remove them it would be as simple as

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