使用 jQuery 更改背景颜色

发布于 2024-10-08 08:54:15 字数 796 浏览 0 评论 0原文

我有一个带有多个下拉菜单的表单...我想在禁用可见下拉菜单时将其背景颜色设置为灰色。所有这些下拉菜单的 id 均以 usrControl 开头。这是我到目前为止所拥有的。

    $('select[id ^= "usrControl"]:visible').each(function(){
this.css("background-color")
});

不知道如何获取禁用/启用的选择器。

提前致谢 alt text

Protected _picklistColorScriptText As String = "$(document).ready(function(){ " + _
                                                "$('[id ^= ""usrControl""]:visible:disabled').css(""background-color"", '#DCDCDC'); " + _
                                                "$('[id ^= ""usrControl""]:visible:enabled').css(""background-color"", '#FFFFFF');" + _
                                                "});"

这有效。但是,这会将整个控件的背景设置为灰色,如何将所选项目的颜色更改为灰色?

I have a form with multiple drop downs... I would like to set the background color of the visible dropdowns to gray when they are disabled. All of these dropdown's id's begin with usrControl. Here is what I have this far..

    $('select[id ^= "usrControl"]:visible').each(function(){
this.css("background-color")
});

Not sure how to get the disabled/enabled selectors.

Thanks in advance
alt text

Protected _picklistColorScriptText As String = "$(document).ready(function(){ " + _
                                                "$('[id ^= ""usrControl""]:visible:disabled').css(""background-color"", '#DCDCDC'); " + _
                                                "$('[id ^= ""usrControl""]:visible:enabled').css(""background-color"", '#FFFFFF');" + _
                                                "});"

This works. However, this sets the background for the entire control to gray, how can I change the color of the selected item to gray?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-10-15 08:54:15

您可以使用 :disabled 伪选择器。另外你不需要 jQuery.each。只需针对返回的节点集进行操作即可。

$('select[id ^= "usrControl"]:visible:disabled').css("background-color", 'gray');

这里的工作示例: http://jsfiddle.net/Cwm4W/

更新:响应下面OP的澄清,您可以只需选择包含的选项,如下所示:

$('select[id ^= "usrControl"]:visible:disabled option').css("background-color", 'gray');

此处的工作示例: http://jsfiddle.net/Cwm4W/1/

You can use the :disabled pseudo-selector. Plus you don't need jQuery.each. Just work against the returned nodeset.

$('select[id ^= "usrControl"]:visible:disabled').css("background-color", 'gray');

Working example here: http://jsfiddle.net/Cwm4W/

UPDATE: Responding to OP's clarification below, you can simply select the contained option like so:

$('select[id ^= "usrControl"]:visible:disabled option').css("background-color", 'gray');

Working example here: http://jsfiddle.net/Cwm4W/1/

一花一树开 2024-10-15 08:54:15

您仅获得 CSS 属性。
试试这个:

$(this).css( "property", "value" );

You are only getting the CSS property.
Try this:

$(this).css( "property", "value" );
他夏了夏天 2024-10-15 08:54:15

我相信你正在寻找

$('select[id ^="usrControl":visible:disabled].each(...

I believe you are looking for

$('select[id ^="usrControl":visible:disabled].each(...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文