代码之家  ›  专栏  ›  技术社区  ›  Tushar Ahirrao

使用类名获取元素

  •  19
  • Tushar Ahirrao  · 技术社区  · 16 年前

    我想要使用类名的元素

    现在我使用的是GWT 2.0

    请帮帮我

    谢谢

    4 回复  |  直到 12 年前
        1
  •  15
  •   danp    13 年前

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

    E:在IE<9中本机不受支持,因此您必须使用如下内容扩展文档/生成全局函数: http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/ 或者用类似的东西 sizzle jquery -感谢下面的评论。

        2
  •  14
  •   Ken Redler    16 年前

    许多解决方案都是针对没有本机的浏览器而构建的。 getElementsByClassName . 如果您使用任何现代的javascript库(例如 jQuery , Prototype ,它们将自动在这些特定于浏览器的间隙上添加雀斑。

    例如,使用jquery:

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

    返回具有类的所有DOM元素 foo ,在任何浏览器中。

    如果您只想解决这个特定的问题,而不想使用完整的库,您可以尝试使用类似 The Ultimate GetElementsByClassName ,这样您就可以:

    getElementsByClassName('foo')
    

    虽然它有几年的历史, John Resig's comparison of various solutions 对于这个问题仍然是有价值的。

        3
  •  3
  •   Daniel De León    14 年前
        4
  •  2
  •   magikMaker    12 年前

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

    看看这里:

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

    推荐文章