#javascript #html #css
#javascript #HTML #css
Вопрос:
Я пытаюсь создать очень простой веб-сайт, и я просто хочу, чтобы два divs менялись, когда я нажимаю на соответствующую кнопку списка сверху.Но второй 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>
Комментарии:
1. myDiv1 не определен. это должна быть строка:
document.getElementById("myDiv1");
2. измененный он все еще не работает. но спасибо!
3. сохраните свой js-скрипт в window.onload
Ответ №1:
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';
}
}
.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;
}
<!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>
d1 = document.getElementById('myDiv1');
d2 = document.getElementById('myDiv2');
getElementById
принимает строку. Вы пропустили одинарные кавычки.
Кроме того, в Javascript нет pass
. Это должно быть return false
Ответ №2:
сначала измените getElementsById(‘Что бы это ни было’):
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>
третий возвращает false в конце функции showDiv1 и showDiv2, чтобы предотвратить применение ссылки
Ответ №3:
возможно, элемент не существует при выполнении вашего 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;
}
}
}
Ответ №4:
Двойное равенство со знаком ‘==’ в функциях было проблемой. Это должно быть просто одно ‘=’. Теперь это работает даже без использования любого из приведенных выше предложений. Спасибо всем 🙂