如何按类型访问 DOM 元素? (类似于 getElementsByName())
不久前,我在JavaScript中进行了一些测试, 并使用代码播放,以获取具有某个类别的所有元素的文本。
现在,我试图做出这样的事情,但通过某种类型获得所有元素,例如所有元素type =“ text”
是否有任何方法可以在JavaScript中执行此操作,或者我应该使用jQuery?
var xx = document.getElementsByClassName("class");
for (i=0;i<xx.length;i++){
var str=xx[i].innerHTML;
alert(str);
}
A while ago I was making some test in JavaScript,
and played with a code to get the text of all elements with a certain class.
Now I was trying to make something like this but obtain all elements by a certain type, for example all elements type="text"
Is there any way to do this in JavaScript or should I use jQuery?
var xx = document.getElementsByClassName("class");
for (i=0;i<xx.length;i++){
var str=xx[i].innerHTML;
alert(str);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在纯老式的JavaScript中,您可以做到这一点:
在JQuery中,您只会做:
In plain-old JavaScript you can do this:
In jQuery, you would just do:
Sizzle Selector Engine(什么功能JQuery)完美地为此做好了准备:
或
The sizzle selector engine (what powers JQuery) is perfectly geared up for this:
Or
如果你很幸运并且只需要关心最近的浏览器,你可以使用:
“recent”表示不是 IE6 和 IE7
If you are lucky and need to care only for recent browsers, you can use:
"recent" means not IE6 and IE7