簡體   English   中英

將不同的樣式應用於asp:DropDownList的選定項

[英]Apply different style to the selected item of the asp:DropDownList

我想設置asp:DropDownList的選定項目的樣式,該項目的其余部分可能具有默認樣式或其他樣式。 請注意,這里的“ 選定項目”是指在選擇之后最終可見的項目

我用過:

ddlID.Items[0].Attributes.CssStyle.Add("background-color", "yellow");

$("#ddlID option:first").css("background-color", "yellow");

ddlID.SelectedItem.Attributes.CssStyle.Add("background-color", "yellow");

$("#ddlID option:selected").css("background-color", "yellow");

上面的任何一項對我來說都不起作用,前兩個樣式是列表的第一項,后兩個樣式是僅在列表中選擇的項。

如果可能,我希望CSS完成任務。

嘗試以下代碼段:

在CSS中

 <style>
        option:checked {
            background-color: yellow;
            color: red;
        }
    </style>

在Aspx頁面中

<asp:DropDownList runat="server">
    <asp:ListItem Text="-- Select --" Value="0"></asp:ListItem>
    <asp:ListItem Text="ListItem1" Value="1"></asp:ListItem>
    <asp:ListItem Text="ListItem2" Value="2"></asp:ListItem>
    <asp:ListItem Text="ListItem3" Value="3"></asp:ListItem>
    <asp:ListItem Text="ListItem4" Value="4"></asp:ListItem>
</asp:DropDownList>

感謝所有這些重要的鏈接和提示。 最后,我得到了解決方案,所以我想在這里分享它。 我剛才說的一類到ASP:DropDownList的 “ddlStyle”添加的所有樣式這一類,我想在那里所選擇的項目(在頂部可見做出選擇后)。

.ddlStyle{
   background-color:yellow;
}

並添加了選項來為asp:DropDownList的項目列表定義其他css屬性

.ddlStyle option{
   background-color:whitesmoke;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM