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

问题描述:

我有这个html,我想将类应用于DIV的第一个按钮。如何将类应用于div的第一个按钮?

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

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

我想为此解决方案使用jQuery。

+0

你想要一个适用于div中第一个按钮的CSS样式吗?然后SleighBoy有答案。 – andho 2009-11-18 08:31:42

使用:first-child选择

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

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

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

哈哈,它很简单,没有jQuery :) +1 – 2009-11-18 07:03:50

+0

我希望它是动态的。 – 2009-11-30 08:15:55

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

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

div.button-container > button:first-child { font-weight: bold; } 

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