HTML切换显示和隐藏

时间:2017-03-01 08:19:03

标签: html css toggle

我开发了一个带有侧边栏标题的HTML文档,我想点击标题,它必须显示和隐藏标题下方的组件。

问题是它只适用于我的“LOGINS”标题。对于我的“什么是出勤”标题,它不显示和隐藏其下面的组件,它隐藏了“登录”组件。因此,根据我的说法,它对“什么是出勤率”标题没有生效。

我还想在每次打开文档时隐藏组件。

另一个问题。每次显示标题下方的组件时,如何使箭头旋转?

这是我到目前为止所做的:

HTML

<script>
function myFunction() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
</script>

<body style=background-image:url('Images/blue.jpg')>

<section id="body" class="width" >


            <aside id="sidebar" class="column-left">
            <img class="thumbnail" src="Images/VsoftLogo.jpg" alt="thumbnail" />

            <h2 onclick="myFunction()" class ="sidebar-heading"><i class="down" style-"top:-50px"></i>   LOGINS</h2>
             <div id="myDIV">
              <nav id="mainnav">
                <ul>
                                    <li><a href="#">Home</a></li>
                                    <li><a href="#">Products</a></li>
                                    <li><a href="#">Solutions</a></li>
                                    <li><a href="#">Contact1</a></li>
                                    <li><a href="#">Contact1</a></li>
                                    <li><a href="#">Contact1</a></li>

                            </ul>
            </nav>  


            </div>


            <h4 onclick="myFunction()" class ="sidebar-heading"><i class="down" style-"top:-50px"></i>   what is attendance</h4>
             <div id="myDIV">
              <nav id="mainnav">
                <ul>
                                    <li><a href="#">Example</a></li>
                                    <li><a href="#">Example2</a></li>
                                    <li><a href="#">Example3</a></li>
                                    <li><a href="#">Example4</a></li>
                                    <li><a href="#">Example</a></li>
                                    <li><a href="#">Contact1</a></li>
                            <hr>                                
                            </ul>
            </nav>  
            </div>

CSS

.mystyle {
    width: 100%;
    padding: 25px;
    background-color: coral;
    color: white;
    font-size: 25px;
}
i {
  border: solid #ffb200;
  border-width: 0 5px 5px 0;
  display: inline-block;
  padding: 5px;

}
.down {
   color:#ffb200;
   transform: rotate(45deg);
   -webkit-transform: rotate(45deg);
   -ms-transform: rotate(45deg);
}

1 个答案:

答案 0 :(得分:1)

您在此处使用id表示多个容器。 DOM中始终id应该是唯一的。

此外,HTML中的语法错误带有style标记。

您可以使用event属性而不是在此处定义ID,而是相应地操作它以在页面加载时默认隐藏。

您可以将类mainnav分配给您的show / hide容器以使其成为

检查fiddle

参考代码:

function myFunction(event) {
  var x = event.target.nextElementSibling,
    y = event.target.firstChild;
  if (x.style.display === 'none' || x.style.display === '') {
    x.style.display = 'block';
    y.classList.add("down");
    y.classList.remove("right");
  } else {
    x.style.display = 'none';
    y.classList.remove("down");
    y.classList.add("right");
  }
}
.sidebar-heading {
  cursor: pointer;
}

.mystyle {
  width: 100%;
  padding: 25px;
  background-color: coral;
  color: white;
  font-size: 25px;
}

i {
  border: solid #ffb200;
  border-width: 0 5px 5px 0;
  display: inline-block;
  padding: 5px;
}

.down {
  color: #ffb200;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
}

.right {
  color: #ffb200;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
}

.mainnav {
  display: none;
}
<body>
  <section id="body" class="width">
    <aside id="sidebar" class="column-left">
      <h2 onclick="myFunction(event);" class="sidebar-heading"><i class="right" style="top:-50px"></i> LOGINS</h2>
      <div class="mainnav">
        <nav>
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Products</a></li>
            <li><a href="#">Solutions</a></li>
            <li><a href="#">Contact1</a></li>
            <li><a href="#">Contact1</a></li>
            <li><a href="#">Contact1</a></li>
          </ul>
        </nav>
      </div>


      <h4 onclick="myFunction(event);" class="sidebar-heading"><i class="right" style="top:-50px"></i> what is attendance</h4>
      <div class="mainnav">
        <nav>
          <ul>
            <li><a href="#">Example</a></li>
            <li><a href="#">Example2</a></li>
            <li><a href="#">Example3</a></li>
            <li><a href="#">Example4</a></li>
            <li><a href="#">Example</a></li>
            <li><a href="#">Contact1</a></li>
            <hr>
          </ul>
        </nav>
      </div>

相关问题