Javascript 使用类名获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3478942/
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-08-23 04:49:16  来源:igfitidea点击:

Get Element By using Class name

javascripthtmlcssdomgwt

提问by Tushar Ahirrao

I want Element by using class name

我想要 Element 使用类名

Now I am using GWT 2.0

现在我正在使用 GWT 2.0

Please help me

请帮我

Thanks

谢谢

采纳答案by dmp

https://developer.mozilla.org/en/DOM/document.getElementsByClassName

https://developer.mozilla.org/en/DOM/document.getElementsByClassName

e: not supported natively in IE < 9, so you'd have to extend document / make a global function with something like this: http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/or use something like sizzleor jquery- thanks to comments below.

e:在 IE < 9 中本机不支持,因此您必须使用以下内容扩展文档/创建全局函数:http: //robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno- 2008/或使用类似sizzlejquery 之类的东西- 感谢下面的评论。

回答by Ken Redler

A number of solutions have been built to work around browsers that don't have native getElementsByClassName. If you use any of the modern javascript libraries (e.g. jQuery, Prototype), they will automatically spackle over these browser-specific gaps.

已经构建了许多解决方案来解决没有本机getElementsByClassName. 如果您使用任何现代 javascript 库(例如jQueryPrototype),它们将自动填补这些特定于浏览器的空白。

So, for example, with jQuery:

因此,例如,使用 jQuery:

$('.foo').get();

returns all the DOM elements with class foo, in any browser.

foo在任何浏览器中返回所有带有 class 的 DOM 元素。

If you only want this particular problem solved, and don't want to use a full library, you can try using something like The Ultimate GetElementsByClassName, which lets you have:

如果您只想解决这个特定问题,并且不想使用完整的库,您可以尝试使用类似The Ultimate GetElementsByClassName 的东西,它可以让您:

getElementsByClassName('foo')

Although it's a couple of years old, John Resig's comparison of various solutionsto the problem is still valuable.

尽管已经有几年的历史了,但John Resig对该问题的各种解决方案的比较仍然很有价值。

回答by Daniel De León

回答by magikMaker

It may be wiser to use document.querySelectoror document.querySelectorAll, supported since IE8.

使用document.querySelector或可能更明智document.querySelectorAll,自 IE8 起支持。

Have a look here:

看看这里:

https://developer.mozilla.org/docs/Web/API/document.querySelectorhttps://developer.mozilla.org/docs/Web/API/document.querySelectorAll

https://developer.mozilla.org/docs/Web/API/document.querySelector https://developer.mozilla.org/docs/Web/API/document.querySelectorAll