简体   繁体   English

按标签名称和类名称获取元素

[英]Get element by tag name and class name

(in vanilla JavaScript) I was wondering if the was an easy way to do something like (在vanilla JavaScript中)我想知道这是否是一种简单的方法

x = document.getElementsByTagName('span') && getElementsByClassName('null');

To return all 'span' elements that have the class name 'null'? 要返回类名为“null”的所有“span”元素?

I thought it might have been something like: 我以为它可能是这样的:

x = document.getElementsByTagName('span'); 
x = x.getElementsByClassName('null');
// or     
x = document.getElementsByTagName('span').getElementsByClassName('null');

But that didn't seem to work out. 但这似乎没有成功。

Is this possible or will I have to iterate through x popping anything that returns false for .class='null'? 这是可能的还是我必须迭代x弹出任何返回false的.class ='null'?

Thanks. 谢谢。

The DOM does not provide any APIs for filtering NodeLists. DOM不提供任何用于过滤NodeLists的API。

Instead, you can use CSS selectors: 相反,您可以使用CSS选择器:

var x = document.querySelectorAll('span.null');

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

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