javascript 您如何仅在 div 内获取按钮?没有 JQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19084509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:17:23  来源:igfitidea点击:

How do you get buttons inside a div only? no JQuery

javascript

提问by y0ruba

<div class="keys">
    <button id="a"></button>
    <button id="b"></button>
</div>

I have a lot of buttons and I only want to getthe ones inside the <div>with class="keys", but I can't get it to work, so far I tried:

我有很多按钮,我只想要withget里面的按钮,但我无法让它工作,到目前为止我尝试过:<div>class="keys"

content = document.getElementsByClassName("keys");
kbButtons = content.getElementsByTagName("button");

and I just get undefined

我只是未定义

回答by Hymanson

Notice how the method is named "getElements...", plural.

请注意该方法如何命名为“getElements...”,复数形式。

document.getElementsByClassName()returns an HTMLCollection, an array-like object.

document.getElementsByClassName()返回一个 HTMLCollection,一个类似数组的对象。

content = document.getElementsByClassName("keys")[0];
kbButtons = content.getElementsByTagName("button");

You can access the first element of the HTMLCollection with the [0]bracket syntax.

您可以使用[0]括号语法访问 HTMLCollection 的第一个元素。

回答by Johannes

I like the querySelector, if you know css selectors this is really powerfull.

我喜欢 querySelector,如果你知道 css 选择器,这真的很强大。

kbButtons = document.querySelectorAll('div.keys button');

kbButtons = document.querySelectorAll('div.keys button');

回答by Justin Russo

Javascript returns an array of dom elements with the function 'getElementsByClassName'.

Javascript 使用函数“getElementsByClassName”返回一个 dom 元素数组。

Thus, if you have one element that you are targeting with your code, then you need to specify the array element you want. Otherwise you're saying, "In this array, give me the button elements," when you should actually be asking, "In this array, give me the button elements in the first element of the array."

因此,如果您的代码针对一个元素,则需要指定所需的数组元素。否则你会说,“在这个数组中,给我按钮元素”,而你实际上应该问,“在这个数组中,给我数组第一个元素中的按钮元素。”

Here's what your javascript code above should look like...

这是你上面的javascript代码应该是什么样子......

content = document.getElementsByClassName("keys");
kbButtons = content[0].getElementsByTagName("button");

for (var iIndex = 0; iIndex < kbButtons.length; iIndex++){
    alert(kbButtons[iIndex].id);
};

回答by Barmar

document.getElementsByClassNamereturns a list of elements, but getElementsByTagNamecan only be applied to a single element, not a list. You need to select one element out of the list:

document.getElementsByClassName返回元素列表,但getElementsByTagName只能应用于单个元素,而不是列表。您需要从列表中选择一个元素:

kbButtons = content[0].getElementsByTagName("button");

回答by Viji

You can try this

你可以试试这个

kbButtons = document.getElementsByClassName('keys')[0].getElementsByTagName('button')