Html 将 css 类应用于 div 内的“按钮”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29647804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:50:10  来源:igfitidea点击:

apply css class to "button" inside div

htmlcss

提问by ScrapCode

I have a div containing a button as below,

我有一个包含如下按钮的 div,

<div id="parentDiv">
    <button id="myBtn" class="myclass">ADD ME</button>
</div>

a new attribute gets added to this button from JS and it becomes like below ('disabled' attribute is added)

JS 向此按钮添加了一个新属性,如下所示(添加了“禁用”属性)

<div id="parentDiv">
    <button id="myBtn" class="myclass" disabled="disabled">ADD ME</button>
</div>

I'm trying to apply below CSS class for disabled button,

我正在尝试在禁用按钮的 CSS 类下面应用,

#parentDiv button.disabled {
    color: #AAAAAA;
}

But to my surprise this class is not getting applied to button. Please point me the correct direction.

但令我惊讶的是,这个类并没有应用于按钮。请指点我正确的方向。

回答by Paulie_D

You need to add the attribute selector

您需要添加属性选择器

#parentDiv button[disabled="disabled"] 
{
    color:red;
}

or just

要不就

#parentDiv button[disabled] 
{
    color:red;
}

#parentDiv button[disabled="disabled"] {
  color: red;
}
<div id="parentDiv">
  <button id="myBtn" class="myclass" disabled="disabled">ADD ME</button>
</div>

回答by Luís P. A.

You can use CSS3 selector :disabled

您可以使用 CSS3 选择器 :disabled

#parentDiv button:disabled
{
    color:#AAAAAA;
}

DEMO HERE

演示在这里

回答by stanze

Replace your styles with this

用这个替换你的样式

#parentDiv button:disabled
{
    color:#F00;
}

回答by panther

Use attribute selector [attr]

使用属性选择器 [attr]

#parentDiv button[disabled] {color: #aaa}

http://jsfiddle.net/f10vfagq/

http://jsfiddle.net/f10vfagq/

回答by Pankaj

#parentDiv button[disabled]
{
    color:red;
}