简体   繁体   English

将CSS应用于禁用按钮

[英]Apply CSS to disabled button

I want to view disabled button as: 我想将禁用按钮查看为:

在此处输入图片说明

The one marked with red is a disabled button and the green one is enabled. 标有红色的是禁用按钮,而已启用的是绿色。

I am trying to apply CSS to do the same as follows: 我正在尝试使用CSS进行如下操作:

 $(document).ready(function () {
     $('#btnSearch').attr("disabled", true);
     $("#btnSearch").css({ 'background': "#grey" });
 });

But its not showing up as mentioned in the image. 但是它没有显示出来,如图中所示。

Its looking for me like this: 它像这样寻找我:

在此处输入图片说明

Which CSS attribute do I need to use for disabled buttons as above (marked in red)? 我需要对上述禁用按钮使用哪个CSS属性(用红色标记)?

JSFIDDLE: JSFIDDLE:

http://jsfiddle.net/54qJx/ http://jsfiddle.net/54qJx/

Try out with prop() 试试看prop()

 $(document).ready(function () {
        $('#btnSearch').prop("disabled", true);
        $("#btnSearch").css({ 'background': "gray" });
 });

@BearGrylls Check this updated fiddle demo , its working as per your requirement. @BearGrylls检查此更新的小提琴演示 ,它可以根据您的要求工作。

You can use opacity css for graying out disabled button : 您可以使用opacity css将disabled按钮变灰:

$('#btnSearch').prop('disabled',true).css('opacity','0.5');

Working Demo 工作演示

you can simply disable a button by this 您可以通过此按钮禁用按钮

$('#btnSearch').prop('disabled', true);

also change the background color with 也用

$('#btnSearch').css('background-color', 'gray');

check the .css() API for usage. 检查.css()API的用法。

btw when using color name like gray, you don't need to put a '#' ahead of the name, that's why your setting of background color doesn't work. 顺便说一句,当使用灰色等颜色名称时,您无需在名称前添加“#”,这就是为什么背景色设置无法正常工作的原因。

css function should be like this to work. CSS函数应该像这样工作。

 $(document).ready(function () {
    $('#btnSearch').prop("disabled", true);
    $("#btnSearch").css('background-color','grey');

});

You can also use attr, but I prefer prop 您也可以使用attr,但我更喜欢prop

 $(document).ready(function () {
        $('#btnSearch').attr("disabled", true);
        $("#btnSearch").css('background-color','#ccc');

    });

You can use the CSS3 :disabled selector, if you want to style it with CSS instead of Javascript: 如果要使用CSS而不是Java来设置样式,则可以使用CSS3:disabled选择器:

#btnSearch:disabled {
   background-color: grey !important;
}

edit: 编辑:

Your problem is, that your button is styled with an inline style -tag. 您的问题是,您的按钮具有内联style -tag style It's generally a bad idea to use these, as it makes maintaining larger pages a nightmare. 使用这些通常是一个坏主意,因为这会使维护较大的页面成为噩梦。 Read this article for more information: Best Practices: Avoid Inline Styles for CSS . 阅读本文以获得更多信息: 最佳实践:避免CSS的内联样式

See this fiddle for an example of how to do it anyways. 有关如何进行操作的示例,请参见此提琴 What I'm doing here is using this trick to override the inline style properties. 我在这里正在使用技巧来覆盖内联样式属性。

Error in line $("#btnSearch").css({ 'background': "#gray" }); $("#btnSearch").css({ 'background': "#gray" });

Try this 尝试这个

Your Button 您的按钮

<button id="btnSearch" onclick="TeacherData_OnSearch()" style="background-color: #249FDA; color: white; height: 22px; width: 45px; border-radius: 4px;">Search</button>

Script 脚本

$(document).ready(function () {
    $('#btnSearch').attr("disabled", true);
    $('#btnSearch').css("background-color", '');
    $("#btnSearch").css({
        'background-color': "grey"
    });

})

DEMO 演示

Setting the background color won't grey won't make the button look like the two you have circled, as it looks like there is more CSS at work then simply setting the background to grey. 设置背景颜色不会变成灰色不会使按钮看起来像您圈出的两个按钮,因为看起来好像有更多的CSS正在工作,然后只需将背景设置为灰色即可。

you can use 您可以使用

$("#btnSearch").css({ "element": "new value" });

But you might need to call it 4 or 5 times to get the desired effect with different elements. 但是您可能需要调用4到5次才能使用不同的元素获得理想的效果。

Otherwise you can use 否则你可以使用

$("#btnSearch").addClass("disabled")

where disabled is a CSS class you've created with the correct styling (background grey, text colour darker etc). 其中Disabled是您使用正确的样式(背景灰色,文本颜色较深等)创建的CSS类。 At a guess the two red circled buttons have had a class added to them to make them look disabled, so you can just add that class to the button you want to disable. 猜测这两个红色圆圈的按钮都添加了一个类,使它们看起来已禁用,因此您可以将该类添加到要禁用的按钮中。

if you want to use "#" with color. 如果您想使用带有颜色的“#”。 than you must use color HEX value not the name 比您必须使用颜色十六进制值而不是名称

set a css class 设置一个CSS类

.bg1 {background:#ddd}

then add it on button 然后将其添加到按钮上

$(document).ready(function () {
        $('#btnSearch').attr("disabled", true);
        $("#btnSearch").addClass("bg1");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM