我们在javascript中有getElementsByClassName吗? [英] Do we have getElementsByClassName in javascript?

查看:121
本文介绍了我们在javascript中有getElementsByClassName吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像在jQuery中我们可以使用$(。classname),javascript中也有类似的东西吗?或者,如果我们没有这样的方法,那么我该如何实现呢。

根据 http://www.dustindiaz.com/getelementsbyclass/ 我将遍历所有标签,然后收集所有与指定类相同的元素。

Just like in jQuery we can use $(".classname"), is there a similar thing in javascript as well? Or if we don't have such method then how can i implement that.
According to http://www.dustindiaz.com/getelementsbyclass/ i will have to iterate through all tags and then collect all elements which are having the same class as specified.

有没有更好的解决方案?

Is there any better solution?

推荐答案

有些浏览器,示例Firefox 3支持 getElementsByClassName ,其他人必须迭代所有标记,因此,如果你想支持所有具有单一功能的浏览器,你应该使用迭代方法。

Some browsers, for example Firefox 3 support getElementsByClassName, others you have to iterate though all the tags, so if you wanted to support all browsers with a single function you should use the iterative method.

最好的解决方案是使用jQuery或任何其他使用最好的框架可用方法。

The best solution is to use jQuery or any other framework which will use the best available method.

这篇关于我们在javascript中有getElementsByClassName吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆