【问题标题】:How to apply class to first button of the div?如何将类应用于 div 的第一个按钮?
【发布时间】:2009-11-18 19:24:15
【问题描述】:

我有这个 html,我想将类应用到 DIV 的第一个按钮。

<div class="button-container">
 <button class="save" />
 <button class="reset" />
</div>

我想给第一个按钮添加类。

我想使用 jQuery 来解决这个问题。

【问题讨论】:

  • 你想要一个应用于 div 中第一个按钮的 CSS 样式吗?那么 SleighBoy 就有了答案。

标签: javascript jquery html css


【解决方案1】:

使用:first-child 选择器

$("div.button-container button:first-child" ).addClass ( "yourclass" );

【讨论】:

    【解决方案2】:
    div.button-container > button:first-child { font-weight: bold; }
    

    来源:http://www.w3.org/TR/CSS2/selector.html#first-child

    【讨论】:

      【解决方案3】:

      你可以看看这个: http://docs.jquery.com/Selectors/firstChild

      $('div button:first-child').addClass(...)

      【讨论】:

        【解决方案4】:

        你可以像这样添加第二个类。

        <div class="button-container">
         <button class="save SecondClass" />
         <button class="reset" />
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-27
          • 1970-01-01
          • 2021-07-13
          • 2022-11-02
          相关资源
          最近更新 更多