利用jquery 实现菜单控制对应视图的显示与隐藏

效果:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="./js/jquery-1.12.4.min.js" ></script>
    <script type="text/javascript">

    $(document).ready(function(){

      $(".top div").click(function(){

        //var arr = $(".top div");

        switch ($(this).index()) {

          case 0:
            console.log(‘0‘);
            $(".top1").addClass("topSelect");
            // 使用siblings获取被操作元素之外的同级元素,然后使用remove()删除
            $(this).siblings().removeClass("topSelect");

            $(".bottom1").removeClass("bottom-no-select").siblings().addClass(‘bottom-no-select‘);

            break;
          case 1:
            console.log(‘1‘);
            $(".top2").addClass("topSelect");
            // 使用siblings获取被点击元素之外的同级元素,然后使用remove()删除
            $(this).siblings().removeClass("topSelect");
            $(".bottom2").removeClass("bottom-no-select").siblings().addClass(‘bottom-no-select‘);

            break;
          case 2:
            console.log(‘2‘);
            $(".top3").addClass("topSelect");
            // 使用siblings获取被点击元素之外的同级元素,然后使用remove()删除
            $(this).siblings().removeClass("topSelect");
            $(".bottom3").removeClass("bottom-no-select").siblings().addClass(‘bottom-no-select‘);

            break;
          case 3:
            console.log(‘3‘);
            $(".top4").addClass("topSelect");
            // 使用siblings获取被点击元素之外的同级元素,然后使用remove()删除
            $(this).siblings().removeClass("topSelect");
            $(".bottom4").removeClass("bottom-no-select").siblings().addClass(‘bottom-no-select‘);

            break;
          default:

        }

        });
      });

      // $(function(){
      //
      // });

    </script>
    <style media="screen">
      .top{
        background-color: yellow;
        width: 100%;
        height: 100px;
        display: flex;
        flex-direction: row;
      }
      .top1{
        background-color: orange;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }
      .top2{
        background-color: orange;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }
      .top3{
        background-color: orange;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }
      .top4{
        background-color: orange;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }

      .bottom{
        background-color: yellow;
        width: 100%;
        height: 200px;
        display: flex;
        flex-direction: row;
      }

      .bottom1{
        background-color: gray;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }

      .bottom11{
        background-color: blue;
        width: 100%;
        height: 100%;
      }

      .bottom2{
        background-color: gray;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }
      .bottom3{
        background-color: gray;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }
      .bottom4{
        background-color: gray;
        width: 100px;
        height: 100%;
        margin-left: 10px;
      }

      .topSelect{
        background-color: red;
      }

      .bottom-no-select{
        /* display:none; */
        /* background-color: rgba(255,255,255,0); */
        opacity: 0;
      }

    </style>
  </head>
  <body>

    <div class="top">
      <div class="top1 topSelect">

      </div>
      <div class="top2">

      </div>
      <div class="top3">

      </div>
      <div class="top4">

      </div>
    </div>

    <div class="bottom">
      <div class="bottom1">
        <div class="bottom11">

        </div>
      </div>
      <div class="bottom2 bottom-no-select">

      </div>
      <div class="bottom3 bottom-no-select">

      </div>
      <div class="bottom4 bottom-no-select">

      </div>
    </div>

  </body>

</html>

$(this).index() 表示同级中当前选取的元素的下标

$(".top1").addClass("topSelect");  表示给某个标签添加一个类

$(this).siblings().removeClass("topSelect"); 使用siblings获取被操作元素之外的同级元素,然后使用remove()删除

$(function(){
   // code fill
});
等价

jQuery(function(){
   // code fill
});
等价

$(document).ready(function () {
    // code fill
});

原文地址:https://www.cnblogs.com/liuw-flexi/p/11994475.html

时间: 2024-08-28 00:32:27

利用jquery 实现菜单控制对应视图的显示与隐藏的相关文章

jQuery将物体居中,并且转换显示和隐藏

今天来给大家贴一段代码,代码的作用就是利用jQuery将物体居中,并且转换显示和隐藏: 首先建立一个div标签并且写好css样式,具体如下 然后我想要的效果是当我点击了button这个按钮,test可以居中显示,第二次点击一次test消失,第三次点击又居中显示,这样循环下去: 具体js代码如下: 稍微解释一下代码的意思: 1.$('#test').get(0).flag = true;是为了建立一个标识,告诉浏览器什么时候应该显示,什么时候应该隐藏 2.上面的top就是让浏览器窗口的高度减去自己

利用来JS控制页面控件显示和隐藏有两种方法

利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一:  1 2 document.getElementById("EleId").style.visibility="hidden"; document.getElementById("EleId").style.visibility="visible"; 利用上述方法实现隐

JS控制HTML元素的显示和隐藏

利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一: document.getElementById("EleId").style.visibility="hidden"; document.getElementById("EleId").style.visibility="visible"; 利用上述方法实现隐藏后,页面

控制页面元素的显示和隐藏

CreateTime--2017年7月13日07:30:55Author:Marydon js控制页面元素的显示和隐藏&<c:if></c:if>标签控制页面元素的显示和隐藏 说明:本文的重点在使用后者实现 实例: 医嘱类型分为:长嘱,临嘱,中医三种,页面默认只显示长嘱信息,点击临嘱或中医显示对应信息:选中变色. CSS部分: /* 医嘱:长嘱,临嘱,中医样式显示 Start*/ .yz_td1 { line-height: 28px; height: 28px; padd

控制div层的显示以及隐藏

控制div层的显示以及隐藏,例如,点击登录,弹出登录的层. 1.添加script代码 <script> function add_address(){ $('#popup2').show(); $('#gray').show(); } function closeAddressDiv(){ $('#popup2').hide(); } </script> 2.在body标签里面添加如下的div <div class="popup2" id="po

JQuery采用CSS实现DOM元素的显示和隐藏

今天参加了一个Code Review活动,看了一个同事的写的代码,感觉满足了当前的功能实现,但是从长远来看,无论从代码复用角度还是维护上看都显得非常差.先看看需求是什么吧.需求是通过下拉框的选择不同的选项,来显示和隐藏一个文本框.同事是这样写的: Code 1 <select name="select" onChange="disinput(this)"> <option value="1">1</option>

iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是 258 2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是 375 英文 2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是

jquery总结06-动画事件01-基础显示和隐藏

动画事件 .hide(option) 动画隐藏  会保存元素的原始属性值 $("#a2").hide({ duration: 3000, complete: function() { alert('执行3000ms动画完毕') } }) .show(option)动画显示 show与hide方法是修改的display属性,通过是visibility属性布局需要通过css方法单独设置 如果使用!important在你的样式中,比如display: none !important,如果你希

Jquery 同个类名中点击的显示其他隐藏的效果

页面的DOM结构如下: <tr> ...... <td><a href="javascript:void(0)" class="c_detail2">查看详情</a></td> </tr> <tr class="detail2"> <td class="intro" colspan="7"> ...... <