JS实现下拉框选中不同的项,对应显示不同的信息

实现的效果如下图:

页面代码

下拉框:

<select id="select3" name="select3" οnchange="showlist()">
   <option value="-1">--请 选择--</option>
   <option value="1">--主题分类--</option>
   <option value="2">--体裁分类--</option>
   <option value="3">--对象分类--</option>

</select>

下面的分类页面代码:

<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr id="zt" style="display:none">
              <td align="left"><asp:Literal ID="Literal1" runat="server"></asp:Literal></td>
            </tr>
            <tr id="tc" style="display:none">
              <td align="left"><asp:Literal ID="Literal2" runat="server"></asp:Literal></td>
            </tr>
            <tr id="dx" style="display:none">
              <td align="left"><asp:Literal ID="Literal3" runat="server"></asp:Literal></td>
            </tr>
          </table>
<script language="javascript" type="text/javascript">
 function changeShow(str)
 {
  if(document.getElementByIdx('sed'+str).style.display == "none")
  {
   document.getElementByIdx('sed'+str).style.display = "block";
   document.getElementByIdx('topimg'+str).src = "../images/minus.gif";
   document.getElementByIdx('topflor'+str).src = "../images/folderopen.gif";
  }
  else
  {
   document.getElementByIdx('sed'+str).style.display = "none";
   document.getElementByIdx('topimg'+str).src = "../images/plusbottom.gif";
   document.getElementByIdx('topflor'+str).src = "../images/folder.gif";
  }
 }
 function changeShow1(str)
 {
  if(document.getElementByIdx('second'+str).style.display == "none")
  {
   document.getElementByIdx('second'+str).style.display = "block";
   document.getElementByIdx('timg'+str).src = "../images/minus.gif";
   document.getElementByIdx('tflor'+str).src = "../images/folderopen.gif";
  }
  else
  {
   document.getElementByIdx('second'+str).style.display = "none";
   document.getElementByIdx('timg'+str).src = "../images/plusbottom.gif";
   document.getElementByIdx('tflor'+str).src = "../images/folder.gif";
  }
 }
 function changeShow2(str)
 {
  if(document.getElementByIdx('secd'+str).style.display == "none")
  {
   document.getElementByIdx('secd'+str).style.display = "block";
   document.getElementByIdx('tim'+str).src = "../images/minus.gif";
   document.getElementByIdx('tfl'+str).src = "../images/folderopen.gif";
  }
  else
  {
   document.getElementByIdx('secd'+str).style.display = "none";
   document.getElementByIdx('tim'+str).src = "../images/plusbottom.gif";
   document.getElementByIdx('tfl'+str).src = "../images/folder.gif";
  }
 }
</script>
需要用到的JS代码:
<script language="javascript" type="text/javascript">
        function showlist()
        {
            var o = document.getElementByIdx('select3');
            var strValue = o.options[o.options.selectedIndex].value;
            if(strValue == "1")
            {
                document.getElementByIdx('zt').style.display = "block";
                document.getElementByIdx('tc').style.display = "none";
                document.getElementByIdx('dx').style.display = "none";
            }
            else if(strValue == "2")
            {
                document.getElementByIdx('zt').style.display = "none";
                document.getElementByIdx('tc').style.display = "block";
                document.getElementByIdx('dx').style.display = "none";
            }
            else if(strValue == "-1")
            {
                document.getElementByIdx('zt').style.display = "none";
                document.getElementByIdx('tc').style.display = "none";
                document.getElementByIdx('dx').style.display = "none";
            }
            else
            {
                document.getElementByIdx('zt').style.display = "none";
                document.getElementByIdx('tc').style.display = "none";
                document.getElementByIdx('dx').style.display = "block";
            }
        }
    </script>

后台数据绑定代码:

 1 #region    主题、体裁、服务对象
 2         void ztlist()
 3         {
 4             数据库操作类实例化 ort ;
 5             StringBuilder sb = new StringBuilder();
 6             sb.Append("<div class=\"divcd\">");
 7             DataTable dt = ort.ExcuteToDataTable("查询语句");
 8             for (int i = 0; i < dt.Rows.Count; i++)
 9             {
10                 if (ort.Record("查询语句")> 0)
11                 {
12                     sb.Append("<div id=\"top" + i.ToString() + "\" class=\"top0\"><img id=\"topimg" + i.ToString() + "\" src=\"../images/plusbottom.gif\" style=\"cursor:hand\" οnclick=\"changeShow('"+i.ToString()+"')\"/><img id=\"topflor" + i.ToString() + "\" src=\"../images/folder.gif\" style=\"cursor:hand\" οnclick=\"changeShow('"+i.ToString()+"')\"/>" + dt.Rows[i]["d_type"].ToString());
13                     sb.Append("<div id=\"sed" + i.ToString() + "\" class=\"sed0\" style=\"display:none\">");
14                     DataTable dts = ort.ExcuteToDataTable("绑定二级目录");
15                     for (int j = 0; j < dts.Rows.Count; j++)
16                     {
17                         sb.Append("<img src=\"../images/page.gif\" /><a href=\"NewsType.aspx?tid="+dts.Rows[j]["d_id"].ToString()+"\">"+dts.Rows[j]["d_type"].ToString()+"</a><br />");
18                     }
19                     sb.Append("</div></div>");
20                     dts.Dispose();
21                 }
22                 else
23                 {
24                     sb.Append("<div id=\"top" + i.ToString() + "\"><img id=\"topimg" + i.ToString() + "\" src=\"../images/minus.gif\" style=\"cursor:hand\"/><img id=\"topflor"+i.ToString()+"\" src=\"../images/folder.gif\" style=\"cursor:hand\"/><a href=\"NewsType.aspx?oid="+dt.Rows[i]["d_id"].ToString()+"\">" + dt.Rows[i]["d_type"].ToString()+"</a></div>");
25                 }
26             }
27             dt.Dispose();
28             Literal1.Text = sb.ToString();
29             ort = null;
30         }
31         void tclist()
32         {
33             实例化数据库操作类 ort ;
34             StringBuilder sb = new StringBuilder();
35             sb.Append("<div class=\"divcd\">");
36             DataTable dt = ort.ExcuteToDataTable("查询语句");
37             for (int i = 0; i < dt.Rows.Count; i++)
38             {
39                 if (ort.Record("查询二级目录(同上)") > 0)
40                 {
41                     sb.Append("<div id=\"first" + i.ToString() + "\" class=\"top0\"><img id=\"timg" + i.ToString() + "\" src=\"../images/plusbottom.gif\" style=\"cursor:hand\" οnclick=\"changeShow1('" + i.ToString() + "')\"/><img id=\"tflor" + i.ToString() + "\" src=\"../images/folder.gif\" style=\"cursor:hand\" οnclick=\"changeShow1('" + i.ToString() + "')\"/>" + dt.Rows[i]["g_type"].ToString());
42                     sb.Append("<div id=\"second" + i.ToString() + "\" class=\"sed0\" style=\"display:none\">");
43                     DataTable dts = ort.ExcuteToDataTable("绑定二级目录");
44                     for (int j = 0; j < dts.Rows.Count; j++)
45                     {
46                         sb.Append("<img src=\"../images/page.gif\" /><a href=\"NewsType.aspx?ttid=" + dts.Rows[j]["g_id"].ToString() + "\">" + dts.Rows[j]["g_type"].ToString() + "</a><br />");
47                     }
48                     sb.Append("</div></div>");
49                     dts.Dispose();
50                 }
51                 else
52                 {
53                     sb.Append("<div id=\"first" + i.ToString() + "\"><img id=\"timg" + i.ToString() + "\" src=\"../images/minus.gif\" style=\"cursor:hand\"/><img id=\"tflor" + i.ToString() + "\" src=\"../images/folder.gif\" style=\"cursor:hand\"/><a href=\"NewsType.aspx?toid=" + dt.Rows[i]["g_id"].ToString() + "\">" + dt.Rows[i]["g_type"].ToString() + "</a></div>");
54                 }
55             }
56             dt.Dispose();
57             Literal2.Text = sb.ToString();
58             ort = null;
59         }
60         void fwlist()
61         {
62             实例化数据库操作类 ort ;
63             StringBuilder sb = new StringBuilder();
64             sb.Append("<div class=\"divcd\">");
65             DataTable dt = ort.ExcuteToDataTable("查询语句");
66             for (int i = 0; i < dt.Rows.Count; i++)
67             {
68                 if (ort.Record("查询语句(同上)") > 0)
69                 {
70                     sb.Append("<div id=\"fit" + i.ToString() + "\" class=\"top0\"><img id=\"tim" + i.ToString() + "\" src=\"../images/plusbottom.gif\" style=\"cursor:hand\" οnclick=\"changeShow2('" + i.ToString() + "')\"/><img id=\"tfl" + i.ToString() + "\" src=\"../images/folder.gif\" style=\"cursor:hand\" οnclick=\"changeShow2('" + i.ToString() + "')\"/>" + dt.Rows[i]["s_type"].ToString());
71                     sb.Append("<div id=\"secd" + i.ToString() + "\" class=\"sed0\" style=\"display:none\">");
72                     DataTable dts = ort.ExcuteToDataTable("绑定二级目录");
73                     for (int j = 0; j < dts.Rows.Count; j++)
74                     {
75                         sb.Append("<img src=\"../images/page.gif\" /><a href=\"NewsType.aspx?ftid=" + dts.Rows[j]["s_id"].ToString() + "\">" + dts.Rows[j]["s_type"].ToString() + "</a><br />");
76                     }
77                     sb.Append("</div></div>");
78                     dts.Dispose();
79                 }
80                 else
81                 {
82                     sb.Append("<div id=\"fit" + i.ToString() + "\"><img id=\"tim" + i.ToString() + "\" src=\"../images/minus.gif\" style=\"cursor:hand\"/><img id=\"tfl" + i.ToString() + "\" src=\"../images/folder.gif\" style=\"cursor:hand\"/><a href=\"NewsType.aspx?foid=" + dt.Rows[i]["s_id"].ToString() + "\">" + dt.Rows[i]["s_type"].ToString() + "</a></div>");
83                 }
84             }
85             dt.Dispose();
86             Literal3.Text = sb.ToString();
87             ort = null;
88         }
89 #endregion

目前做的这个只支持二级目录,如果有兴趣的话,可以进行改造。

 

 

转载于:https://www.cnblogs.com/Chaser-Eagle/p/3684714.html

前端开发中,特别是在使用JavaScript框架如React、Vue或Angular时,实现下拉框(通常称为Select控件)的不同不同的背景色,可以通过以下几个步骤: 1. **HTML结构**: 首先,创建一个基本的下拉框,并给每个option元素分配唯一的`value`属性和一个内部的`data-background-color`属性用于存储背景色信息,例如: ```html <select id="colorDropdown"> <option value="1" data-background-color="#fff">选1</option> <option value="2" data-background-color="#eee">选2</option> <!-- 更多选 --> </select> ``` 2. **CSS样式**: 定义一个基础的选中状态样式,然后利用`:not(:selected)`伪类选择未选中的选,以及`:hover`伪类来改变鼠标悬停时的背景色,例如: ```css select option { background-color: #fff; } select option:not(:selected):hover { background-color: #ddd; } ``` 如果需要为特定的选设置特殊颜色,可以添加额外的类名并编写对应的选择器。 3. **JavaScript或jQuery (如果使用)**: 如果你想动态地根据用户交互或数据绑定调整背景色,可以使用JavaScript监听`change`事件,获取当前选中的option并修改其背景色。例如,在React中: ```jsx function handleOptionChange(e) { const selectedOption = document.getElementById('colorDropdown').options[e.target.selectedIndex]; selectedOption.style.backgroundColor = selectedOption.dataset.backgroundColor; } // 绑定事件处理函数 document.getElementById('colorDropdown').addEventListener('change', handleOptionChange); ``` 同样的,使用其他库也类似,只是API会有所不同
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值