How to apply class to first button of the div?
I 开发者_StackOverflowhave this html and I want to apply class to first button of the DIV.
<div class="button-container">
<button class="save" />
<button class="reset" />
</div>
I want to add class to the first button.
I want to use jQuery for this solution.
Use the :first-child selector
$("div.button-container button:first-child" ).addClass ( "yourclass" );
div.button-container > button:first-child { font-weight: bold; }
Source: http://www.w3.org/TR/CSS2/selector.html#first-child
You could look at this: http://docs.jquery.com/Selectors/firstChild
$('div button:first-child').addClass(...)
You can add a second class like this.
<div class="button-container">
<button class="save SecondClass" />
<button class="reset" />
</div>
精彩评论