如何将类应用于div的第一个按钮?

Was*_*ikh 0 html javascript css jquery

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

<div class="button-container">
 <button class="save" />
 <button class="reset" />
</div>
Run Code Online (Sandbox Code Playgroud)

我想将类添加到第一个按钮.

我想在这个解决方案中使用jQuery.

rah*_*hul 6

使用 :first-child 选择器

$("div.button-container button:first-child" ).addClass ( "yourclass" );
Run Code Online (Sandbox Code Playgroud)


Jam*_*ack 5

您可以看一下:http : //docs.jquery.com/Selectors/firstChild

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


Sle*_*Boy 5

div.button-container > button:first-child { font-weight: bold; }
Run Code Online (Sandbox Code Playgroud)

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