简体   繁体   English

Jquery 或 Javascript 仅更改所选 DropDownList 项的文本颜色

[英]Jquery or Javascript to change just the text color of a selected DropDownList Item

I can't seem to figure this out.我似乎无法弄清楚这一点。 I have an asp DropDownList control.我有一个asp DropDownList 控件。 This gets populated by a class list of data I get from SQL.这由我从 SQL 获得的数据的 class 列表填充。 The DropDownList is in an ASP Repeater. DropDownList 位于 ASP 中继器中。 I added an OnSelectedIndexChanged property to the asp control that fires a C# method.我向触发 C# 方法的 asp 控件添加了 OnSelectedIndexChanged 属性。 I have some logic in the initializing method for the DropDownList that sets a specific item's text color to red (depending on a value in that items class).我在 DropDownList 的初始化方法中有一些逻辑,它将特定项目的文本颜色设置为红色(取决于该项目类中的值)。 Whenever that item is selected the text is not red. Whenever that item is selected the text is not red. The text is only red when it is an item within the list.只有当它是列表中的一个项目时,文本才会显示为红色。 How can I change the color of the selected item?如何更改所选项目的颜色? I cannot seem to figure this out.我似乎无法弄清楚这一点。 I need the OnSelectedIndexChanged method on the C# side to fire an event to filter another selector.我需要 C# 端的 OnSelectedIndexChanged 方法来触发事件以过滤另一个选择器。 So I am already firing an event when the selector is changed. So I am already firing an event when the selector is changed. However I have tried the following:但是我尝试了以下方法:

protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
{
    if (sender != null)
    {
        DropDownList DropDownList1 = (DropDownList)sender;
        DropDownList1.SelectedItem.Attributes.Add("color", "Red;");
    }
}

The above does not work.以上不起作用。 But I don't think that is the issue.但我不认为这是问题所在。 After inspecting the element (before the above implementation) the style color property is already set to red.检查元素后(在上述实现之前),样式颜色属性已设置为红色。 Do these just not apply to the currently selected item?这些只是不适用于当前选定的项目吗?

I was able to set the text color of the entire list by setting the forecolor.我可以通过设置前景色来设置整个列表的文本颜色。 But I can't set a single element's forecolor.但我不能设置单个元素的前景色。

I don't think you can generally do this, apart from in some of the latest versions of firefox and chrome.除了 firefox 和 chrome 的一些最新版本之外,我认为您通常无法做到这一点。 Traditionally we've never had much styling control on the HTML SELECT element, as they were originally just a standard desktop UI widget schlepped into the page.传统上,我们从来没有对 HTML SELECT 元素进行太多样式控制,因为它们最初只是一个标准的桌面 UI 小部件固定在页面中。

See this answer for more details: How to style a <select> dropdown with CSS only without JavaScript?有关更多详细信息,请参阅此答案: How to style a <select> dropdown with CSS only without JavaScript?

That said, are you sure you should not be setting the Style["color"] on the option , rather than an attribute ?也就是说,您确定不应该在option上设置Style["color"]而不是attribute吗?

Here is a jsfiddle with option tags that are styled with a foreground color.是一个带有前景色样式的选项标签的 jsfiddle。 The way Chrome appears to work, the option color only matters when the dropdown is open. Chrome 似乎工作的方式,选项颜色仅在下拉菜单打开时才重要。

Select controls are often rendered by the operating system (rather than the browser) and the implementation varies widely from browser to browser and operating system to operating system. Select 控件通常由操作系统(而不是浏览器)呈现,并且实现因浏览器和浏览器以及操作系统而异。 As such, it is often impossible to reliably style a select control using CSS.因此,通常不可能使用 CSS 可靠地设置 select 控件的样式。

However, there are a number of techniques available to get around this limitation.但是,有许多技术可以解决这个限制。 One of the most common is to replace the select with a dynamic HTML list.最常见的一种是将 select 替换为动态 HTML 列表。 You can use a combination of CSS and Javascript to cause the list to operate in a manner very similar to a select control.您可以使用 CSS 和 Javascript 的组合使列表以非常类似于 select 控件的方式运行。

There are a number of jQuery plugins available that do the hard work for you.有许多 jQuery 插件可以为您完成艰苦的工作。 You can use this page as a starting point.您可以使用此页面作为起点。

DropDownList1.SelectedItem.Attributes.CssStyle.Add("color", "red"); DropDownList1.SelectedItem.Attributes.CssStyle.Add("color", "red");

Worked when I tried it;当我尝试它时工作; it changed the color of the selected item.它改变了所选项目的颜色。

    <asp:DropDownList ID="ddlItem" runat="server" AutoPostBack="True" 
        onselectedindexchanged="ddlItem_SelectedIndexChanged">
    <asp:ListItem Text="hi" Value="hi" />
    <asp:ListItem Text="there" Value="there" />
    <asp:ListItem Text="testing" Value="testing" />
    </asp:DropDownList>

    protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (sender != null)
        {
            DropDownList DropDownList1 = (DropDownList)sender;
            DropDownList1.SelectedItem.Attributes.CssStyle.Add("color", "red");
        }

    }

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

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