为什么我不能让div改变onclick?

时间:2016-10-20 09:53:49

标签: javascript html css

我试着创建一个非常简单的网站,我只想在我点击顶部的相应列表按钮时更改两个div。但是,当我点击第二个列表按钮时,第二个div永远不会出现。无法弄清楚为什么现在几个小时,代码中的所有步骤看起来都是合乎逻辑的。我想在没有Jquery或其他任何东西的情况下这样做。



window.onload = function() {
d1 = document.getElementById('myDiv1');
d2 = document.getElementById('myDiv2');
function showDiv1() {
  if( d2.style.display == "block" )
  {
    d2.style.display = "none";
    d1.style.display = "block";
  }
  else
  {
    return false;
  }
}

function showDiv2() {
  if( d1.style.display == "block" )
  {
    d1.style.display = "none";
    d2.style.display = "block";
  }
  else
  {
    return false;
  }
}
}

body {
  margin: 0;
  padding: 0;
  background-repeat: no-repeat;
  background-position: center;
  text-align: center;
  font-size: 100%;
  height: 100%;
}

.myDiv1, .myDiv2 {
  background-image: url("paper.jpg");
  background-color: none;
  margin: auto;
  height: 80%;
  border: 2px solid black;
  border-radius: 0px;
  text-align: left;
  /*color: white;
  font-family: "Arial", Times, serif;
  font-weight: bold;*/
  padding: 10px;
  overflow: auto
}

.myDiv1 {
  display: block;
}

.myDiv2 {
  display: none;
}

p {

}


.ul1 {
  list-style-type: none;
  margin: 0px;
  padding: 0px;
  overflow: hidden;
  background-color: #dddddd;
  height: 70px;
}
.li1 {
  float: right;
}
.li1 a {
  display: block;
  color: black;
  text-decoration: none;
  padding: 25px    
}
.li1:first-child {
  margin-right: 100px;
}

/* Grow */
.hvr-grow {
  display: inline-block;
  vertical-align: middle;
  transform: translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  backface-visibility: hidden;
  -moz-osx-font-smoothing: grayscale;
  transition-duration: 0.3s;
  transition-property: transform;
}

.hvr-grow:hover,
.hvr-grow:focus,
.hvr-grow:active {
  transform: scale(1.1);
}

.ul2 {
  list-style-type: none;
  margin: 20px auto;
  padding: 0px;
  overflow: hidden;
  background-color: black;
  border: 1px solid black;
  border-radius: 20px;
  width: auto;
  display: flex;
}
.li2 {
  display: inline;
  border-right: 1px solid white;
  width: 25%
}
.li2:last-child {
  border-right: none;
}
.li2 a {
  color: white;
  text-decoration: none;
}

.ul3 {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
.li3 {
  margin: 15px 0;
}

#myDiv1:checked ~ #myDiv .inner { margin-left:0; }
#myDiv2:checked ~ #myDiv .inner { margin-left:-100%; }
#myDiv3:checked ~ #myDiv .inner { margin-left:-200%; }

<ul class="ul1">
  <li class="li1 hvr-grow"  ><a href="#sport" onclick="showDiv1()">SPORT</a>
  </li>
  <li class="li1 hvr-grow"><a href="#sportler" onclick="showDiv2()">SPORTLER</a>
  </li>
  <li class="lix">Meine Webseite</li>
</ul>


<div class="myDiv1" id="myDiv1">
  <ul class="ul3">
    <li class="li3"><a href="default.asp">Icon1</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="news.asp">Icon2</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="contact.asp">Icon3</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon4</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon5</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon6</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon7</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon8</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon9</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon10</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon11</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon12</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon13</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon14</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon15</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon16</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon17</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon18</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon19</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
    <li class="li3"><a href="about.asp">Icon20</a></li>
    <p  style="font-size: 0.85em;">This is the description of this sport</p>
  </ul>
</div>
<div class="myDiv2" id="myDiv2">my name</div>

<ul class="ul2">
  <li class="li2"><a href="#salman">Salman</a>
  </li>
  <li class="li2"><a href="#Patric">Patric</a>
  </li>
  <li class="li2"><a href="#IMIBE">IMIBE</a>
  </li>
  <li class="li2"><a href="#CUE">CUE</a>
  </li>
</ul>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

&#13;
&#13;
d1 = document.getElementById('myDiv1');
d2 = document.getElementById('myDiv2');

function showDiv1() {
	if (d1.className.indexOf('hide' > -1)) {
			d1.className.replace('hide', '');
            d1.className += ' show';
		}

	}

	function showDiv2() {
		if (d2.className.indexOf('hide' > -1)) {
				d2.className.replace('hide', '');
          d2.className += ' show';
			}
		}
&#13;
.ul2 {
  list-style-type: none;
  margin: 20px auto;
  padding: 0px;
  overflow: hidden;
  background-color: black;
  border: 1px solid black;
  border-radius: 20px;
  width: auto;
  display: flex;
}
.li2 {
  display: inline;
  border-right: 1px solid white;
  width: 25%
}
.li2:last-child {
  border-right: none;
}
.li2 a {
  color: white;
  text-decoration: none;
}
.ul3 {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
.li3 {
  margin: 15px 0;
}
.hide {
  display: none;
}
&#13;
<!DOCTYPE html5>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="myStyle.css">
</head>

<body>

  <ul class="ul1">
    <li class="li1 hvr-grow"><a href="#sport" onclick="showDiv1()">SPORT</a>
    </li>
    <li class="li1 hvr-grow"><a href="#sportler" onclick="showDiv2()">SPORTLER</a>
    </li>
    <li class="lix">Meine Webseite</li>
  </ul>


  <div class="myDiv1 hide" id="myDiv1">
    <ul class="ul3">
      <li class="li3"><a href="default.asp">Icon1</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="news.asp">Icon2</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="contact.asp">Icon3</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon4</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon5</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon6</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon7</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon8</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon9</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon10</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon11</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon12</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon13</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon14</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon15</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon16</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon17</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon18</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon19</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
      <li class="li3"><a href="about.asp">Icon20</a>
      </li>
      <p style="font-size: 0.85em;">This is the description of this sport</p>
    </ul>
  </div>
  <div class="myDiv2 hide" id="myDiv2">my name</div>

  <ul class="ul2">
    <li class="li2"><a href="#salman">Salman</a>
    </li>
    <li class="li2"><a href="#Patric">Patric</a>
    </li>
    <li class="li2"><a href="#IMIBE">IMIBE</a>
    </li>
    <li class="li2"><a href="#CUE">CUE</a>
    </li>
  </ul>


</body>

</html>  
&#13;
&#13;
&#13;

 d1 = document.getElementById('myDiv1');
 d2 = document.getElementById('myDiv2');

getElementById接受一个字符串。你错过了单引号。

此外,Javascript中没有pass。它应该是return false

答案 1 :(得分:0)

首先更改getElementsById(&#39; whateverTheIdIs&#39;):

d1 = document.getElementById('myDiv1');
d2 = document.getElementById('myDiv2');

将onClick事件绑定到相应的元素:

<li class="li1 hvr-grow"  ><a href="#sport" onclick="showDiv1()">SPORT</a>
</li>
<li class="li1 hvr-grow"><a href="#sportler" onclick="showDiv2()">SPORTLER</a>
</li>

第三次在showDiv1和showDiv2函数末尾返回false以防止链接被应用

答案 2 :(得分:0)

当你的js代码执行时,这个元素可能不存在 解决那个问题 只需将你的js脚本存储在onload事件

window.onload = function() {

    d1 = document.getElementById('myDiv1');
    d2 = document.getElementById('myDiv2');
    function showDiv1() {
       if( d2.style.display == "block" )
       {
          d2.style.display = "none";
          d1.style.display = "block";
       }
       else
       {
          pass;
       }
    }

    function showDiv2() {
       if( d1.style.display == "block" )
       {
          d1.style.display = "none";
          d2.style.display = "block";
       }
       else
       {
          pass;
       }
    }
}

答案 3 :(得分:0)

双等于符号&#39; ==&#39;在功能上是问题。它应该只是一个&#39; =&#39;。现在即使不使用上述任何建议也能正常工作。谢谢大家:)