• JS练习题(左侧菜单下拉+好友选中)


    题一、左侧菜单下拉

    做题思路:先做菜单和子菜单,把子菜单默认隐藏。再用JS调样式。

    <style type="text/css">
    *{ margin:0px auto; padding:0px}
    .text1{ 180px; height:39px; background-color:rgba(0,59,102,1); border-bottom:1px solid white; text-align:center; line-height:40px; vertical-align:middle; color:white}
    .text1:hover{ cursor:pointer}
    .text2{ 160px; height:40px; float:left; font-family:微软雅黑}
    .text2:hover{ color:#F00}
    .text3{ 20px; height:40px; float:left; color:white}
    .text4{ 178px; height:40px; text-align:center; line-height:40px; vertical-align:middle; border-left:1px solid #999; border-right:1px solid #999}
    .zi{ display:none}//所有的子菜单隐藏
    #z33{ border-bottom:1px solid #999}
    </style>
    </head>
    
    <body>
    <div style="500px; height:500px">
    <div id="shouye" class="text1">
    <div class="text2" onclick="Show()">首页</div>
    <div class="text3">></div>
    </div>
    
    <div id="jiaowu" class="text1"; onclick="Show('z1')">
    <div class="text2">教务信息</div>
    <div class="text3">></div>
    </div>
    <div class="zi" id="z1">
    <div class="text4">查看通知</div>
    <div class="text4">发送通知</div>
    </div>
    
    <div id="xiazai" class="text1" ; onclick="Show('z2')">
    <div class="text2">下载专区</div>
    <div class="text3">></div>
    </div>
    <div class="zi" id="z2">
    <div class="text4">简历文件下载</div>
    <div class="text4">教师评测下载</div>
    <div class="text4">其它下载</div>
    </div>
    <div id="xueyuan" class="text1" ; onclick="Show('z3')">
    <div class="text2">学员信息管理</div>
    <div class="text3">></div>
    </div>
    <div class="zi" id="z3">
    <div class="text4">项目</div>
    <div class="text4">考试</div>
    <div class="text4" id="z33">作业</div>
    </div>
    
    </div>
    
    </body>
    <script type="text/javascript">
    function Show(id)//用变量id(形参)接收根据接收的变量找到要操作的项。
    {
      var z = document.getElementById(id);//点击主菜单,子菜单显示
      if(z.style.display=="block")//如果原先子菜单是显示的
      {
        z.style.display="none";//让原先显示的隐藏
      }
      else//如果原先是隐藏的
      {
        z.style.display="block";//让原先隐藏的显示
      }
    }
    
    </script>

    题二、好友列表选中

    *{ margin:0px auto; padding:0px}
    .text{ 150px; height:35px; border:2px solid white; background-color:#03F; text-align:center; line-height:30px; vertical-align:middle; color:white}
    .text:hover{ cursor:pointer;}<!--用hover改变颜色后面鼠标放上后颜色会无效,下面用的onmouseover变色鼠标放上后的背景色。-->
    </style>
    </head>
    
    <body>
    <div style="500px; height:500px; margin-top:30px">
    <div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">唐僧</div>
    <div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">孙悟空</div>
    <div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">猪八戒</div>
    <div class="text" onclick="Show(this)" onmouseover="Bian(this)" onmouseout="Hui(this)" xz="0">沙僧</div>
    </div>
    </body>
    <script type="text/javascript">
    function Show(a)//用一个形参接收变量
    {
      var attr = document.getElementsByClassName("text");//找到所有的好友
      for(i=0;i<attr.length;i++)
      {
        attr[i].style.backgroundColor="#63C";//先背景色设置为是默认的颜色,防止点击一个之前点击更换的背景色不变回默认的颜色。
        attr[i].setAttribute("xz","0");//清别的选项的背景色,设置为0.
      }
      a.setAttribute("xz","1");//代表选中,设置为1.
      a.style.backgroundColor="#F63";//点击换背景色
    }
    function Bian(a)//鼠标放上以后背景色改变,但是原先鼠标选定的背景颜色会改变成默认颜色。
    {
      var attr = document.getElementsByClassName("text");
      for(i=0;i<attr.length;i++)
      {
        if(attr[i].getAttribute("xz")=="0")//要在前面加上xz的属性,默认等于0。
        {
          attr[i].style.backgroundColor="#63C";
        }    
      }
      a.style.backgroundColor="#F63";    
    }
    function Hui(a)//鼠标离开后颜色返回默认颜色
    {
      var attr = document.getElementsByClassName("text");
      for(i=0;i<attr.length;i++)
      {
        if(attr[i].getAttribute("xz")=="0")
        attr[i].style.backgroundColor="#63C";
      }    
    }
    </script>
  • 相关阅读:
    ActiveMQ基础教程JMS概述
    spring JMS在接收消息的时候总是报错
    java中实现同步的两种方式:syschronized和lock的区别和联系
    JMS之——ActiveMQ时抛出的错误Could not connect to broker URL-使用线程池解决高并发连接
    jQuery 实现 bootstrap 模态框 删除确认
    Jsp使用遍历List集合
    list里面放的实体对象,页面用c:foreach应该怎么取?
    如何停止处于stopping状态的windows服务(使用taskkill)
    Editplus配置Python的开发环境
    NAT对于网络上的其它人都不可见(但可以转发端口),Bridged和网络里的其它物理机没区别,Internal只能在虚拟机之间可见,Host-only只对主机可见(最符合我的服务器需求),最多可设置8块网卡实现混杂
  • 原文地址:https://www.cnblogs.com/Strive-count/p/5905545.html
Copyright © 2020-2023  润新知