onmouseover和onmouseout应用于RadioButtonList或CheckBoxList
一直想实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上,今晚终于有时间实现它。此功能就是当鼠标经过时RadioButtonList或CheckBoxList每一个Item时,让Item有特效显示,离开时,恢复原样。可以看到效果:
RadioButtonList效果:
CheckBoxList效果:
这资实现数据,Insus.NET准备了五行(Five Phases)
创建一个对象[Five Phases]:

using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for FivePhases /// </summary> public class FivePhases { private int _ID; private string _Name; public int ID { get { return _ID; } set { _ID = value; } } public string Name { get { return _Name; } set { _Name = value; } } public FivePhases() { // // TODO: Add constructor logic here // } public FivePhases(int id, string name) { this.ID = id; this._Name = name; } }

private List<FivePhases> GetFivePhases() { List<FivePhases> ListFH = new List<FivePhases>(); FivePhases fh = new FivePhases(); fh.ID = 1; fh.Name = "木"; ListFH.Add(fh); fh = new FivePhases(); fh.ID = 2; fh.Name = "火"; ListFH.Add(fh); fh = new FivePhases(); fh.ID = 3; fh.Name = "土"; ListFH.Add(fh); fh = new FivePhases(); fh.ID = 4; fh.Name = "金"; ListFH.Add(fh); fh = new FivePhases(); fh.ID = 5; fh.Name = "水"; ListFH.Add(fh); return ListFH; }
此时,你可以拉一个RadioButtonList或是CheckBoxList控件至网页中,此例以RadioButtonList控件为例。

<asp:CheckBoxList ID="RadioButtonListFivePhases" runat="server" RepeatDirection="Horizontal"></asp:CheckBoxList>
然后在cs绑定数据:

using System.Data.OleDb; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Insus.NET; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Data_Binding(); } private void Data_Binding() { this.RadioButtonListFivePhases.DataSource = GetFivePhases(); this.RadioButtonListFivePhases.DataTextField = "Name"; this.RadioButtonListFivePhases.DataValueField = "ID"; this.RadioButtonListFivePhases.DataBind(); } }
还得准备鼠标的over与out样式:

<style type="text/css"> .overStyle { font-weight: bold; color: #f00; } .outStyle { font-weight: normal; color: none; } </style>
在Javascript中实现每个Item有onmouseover和onmouseout事件,因此还得写Javascript脚本,放于<head>内。

<script type="text/javascript"> function windowOnLoad() { var rbl = document.getElementById('<%= RadioButtonListFivePhases.ClientID %>'); var labels = rbl.getElementsByTagName('label'); for (var i = 0; i < labels.length; i++) { var lbl = labels[i]; lbl.onmouseover = function () { this.className = 'overStyle'; }; lbl.onmouseout = function () { this.className = 'outStyle'; }; } } window.onload = windowOnLoad; </script>
标签:
CSS
, js
, JavaScript
, onload
, onmouseover
, RadioButtonList
, onmouseout
, CheckBoxList
, getElementById
, getElementsByTagName
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2010-01-23 ASP.NET教程7
2010-01-23 ASP.NET教程6
2010-01-23 ASP.NET教程5
2010-01-23 ASP.NET教程4
2010-01-23 ASP.NET教程2
2010-01-23 ASP.NET教程3
2010-01-23 ASP.NET教程1