【问题标题】:What is the jQuery selector for selecting anchor elements with a particular class in a particular div什么是用于在特定 div 中选择具有特定类的锚元素的 jQuery 选择器
【发布时间】:2011-07-01 02:00:52
【问题描述】:

我有一些这样的代码,我想在 div foo 中选择每个 <a>status 的标签

<div id="foo">
...
<a class = "status"> ... </a>
...
</div>

【问题讨论】:

  • 看来您忘记了结束语

标签: javascript jquery performance selector


【解决方案1】:

选择器是:

$("#foo a.status");

【讨论】:

    【解决方案2】:

    试试这个并阅读this

    $("#foo a.status")
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      试试

      $('div#foo > a.status')
      

      它选择作为 div #foo 的 DIRECT 子级的锚

      【讨论】:

      • 注意:这将只选择具有status 类的锚点,这些锚点是id 为foo 的div 的直接子代。这可能是 OP 所追求的,我只是想在这里澄清一下。
      • @Alex thanx,我会将其添加到答案中
      【解决方案4】:

      试试这个:

      $('div#foo a.status')
      

      文档是here

      【讨论】:

        【解决方案5】:

        这行得通。

        $("#foo").find("a.status")
        

        【讨论】:

          【解决方案6】:
          jQuery('#foo')     //select elm with id foo
          .find('a.status')  //find anchor with class
          

          【讨论】:

            【解决方案7】:

            你可以这样做$('#foo').find('.status')

            【讨论】:

            • hasClass 返回布尔值,而不是实际具有该类的元素。无论如何,链接很棒。
            【解决方案8】:

            没有“jQuery 选择器”之类的东西,你的意思是:

            任一 CSS 选择器

            在这种情况下,答案是div#foo a.statusdiv#foo &gt; a.status(取决于是否有中间容器)

            jQuery函数

            在这种情况下,有几种方法可以做到:

            • $('div#foo a.status')
            • $('div#foo &gt; a.status')
            • $('div#foo').find('a.status')
            • $('div#foo').children('a.status')

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-12-05
              • 2011-03-02
              • 2011-01-10
              • 1970-01-01
              • 1970-01-01
              • 2016-06-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多