DropDownList中的更改背景颜色选择的项目在ASP.NET中使用CSS [英] Change background colour of dropdownlist selected item in ASP.NET with CSS

查看:1901
本文介绍了DropDownList中的更改背景颜色选择的项目在ASP.NET中使用CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改选定项的背景颜色dropdownlists,列表框,和文本框。这可能与CSS?

I would like to change the background colour of the selected item in dropdownlists, listboxes, and textboxes. Is this possible with CSS?

感谢

更新

只是尝试这样的CSS:

Just tried this CSS:

::-moz-selection 
{
    background-color:#8CCA1E;
    color:Black;
}

伟大工程选定的文本,但不是为dropdownlists或列表框悬停颜色或所选项目的颜色。

Works great for selected text but not for hover colours or selected item colours on dropdownlists or listboxes.

推荐答案

您可以通过指定CssClass属性,或者背景色属性设置在.aspx页面中的背景颜色..它看起来像:

You can set background colors from the .aspx page by specifying either the CssClass, or the BackColor property.. it looks like:

<asp:ListBox CssClass="MyListBox" BackColor="#e0e0e0"></asp:ListBox>

设置所选的项目是有点麻烦...我不相信这是直接针对于此的属性。你可以将其设置成JavaScript或jQuery的,是这样的:

Setting the selected item is a little trickier... I don't believe there is an attribute for this directly. You can set it in javascript, or jQuery, something like:

// Never mind, this won't work
<script type="text/javascript">
  $(document).ready(function() {
    $('#MyListBox').click(function() {
        $("#MyListBox option:selected").css("background-color", "#e0e0e0");
    }); 
  });
</script>

我会测试,以确保它的工作原理...如果你需要一个直接的JavaScript的解决方案,我要看看我能想出。但是,如果你不使用jQuery,你想在你的控制细粒度的控制,现在是学习的好时机:)

I'll test that to make sure it works... if you need a straight javascript solution, I'll see what I can come up with. But if you're not using jQuery, and you want fine-grained control over your controls, now is a good time to learn it : )

感谢贾斯汀进行更正的脚本!

Thanks to Justin for corrections to the script!



修改

哈弗是容易...添加样式:


EDIT
Hover is easy... add a style:

  select > option:hover {
    background-color:  #ffffd0;
  }

这将设置在鼠标悬停在页面上的任何列表框项目的背景颜色。 jQuery的可以用来设置点击功能上的所有列表框,而不仅仅是具体#MyListBox,给我几分钟,我将添加一些code

That will set the background color on any listbox item you hover over on the page. jQuery can be used to set the click function on all listboxes, not just specifically #MyListBox... give me a couple of minutes, I'll add some code



更新

经过广泛的研究,倾尽所有资源我也可以把我的手,最好的答案,我想出是:设置所选的颜色无法做到的。


UPDATE
After extensive research, exhausting every resource I had or can put my hands on, the best answer I come up with is: Setting the selected color can't be done.

甚至不是我想出了jQuery的code会工作。所选的颜色由OS设置,并且不能被重写。

Not even the jQuery code I came up with will work. The selected color is set by the OS, and can't be overridden.

对不起,最好我能做的是:伪选择悬停

Sorry, best I can do is the :hover pseudo-selector
: (

这篇关于DropDownList中的更改背景颜色选择的项目在ASP.NET中使用CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆