2017-03-17 83 views
1

我正在使用html和css创建一个大学项目的网站,我试图添加一个下拉菜单。我从w3Schools跟随了这个教程,但由于某种原因,当我点击按钮打开菜单时,它不会保持打开状态并立即消失。我已经尽可能地查看了我的代码,但我看不出它有什么问题。由于点击它时,HTML下拉菜单不会保持打开状态

这里是我的CSS代码:

body{ 
    background-color: black; 
} 

header, footer{ 
    color: black; 
    background-color: red; 
    padding: 1em; 
    text-align: center; 
} 

.dropbtn{ 
    background-color: #FF0000; 
    color: black; 
    padding: 8px; 
    font-size: 12px; 
    border: none; 
    cursor: pointer; 
} 

.confirmbtn { 
    margin: 10px 0; 
} 

.dropbtn:hover, .dropbtn:focus { 
    background-color: #B20000; 
} 

.dropdown{ 
    position: relative; 
    display: inline-block; 
} 

.dropdown-content { 
    display: none; 
    position: absolute; 
    background-color: #F6F6F6; 
    min-width: 50px; 
    overflow: auto; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
    z-index: 1; 
} 

.dropdown-content a { 
    color: black; 
    padding: 12px 16px; 
    text-decoration: none; 
    display: block; 
} 

.dropdown a:hover { background-color: #f1f1f1; } 

.show { display: block; } 

article{ 
    margin-left: 200px; 
    border-left: 2px solid red; 
    border-right: 2px solid red; 
    padding: 1em; 
    height: 600px; 
    background-color: black; 
    color: red; 
    z-index: 1; 
} 

form{ 
    text-align: center; 
} 

textarea{ 
    display: block; 
    margin-left: auto; 
    margin-right: auto; 
} 

li{ 
    margin: 10px 0; 
} 

image{ 
    height: 100px; 
} 

nav{ 
    float: left; 
    border-left: 2px solid red; 
    max-width: 190px; 
    padding: 1em; 
    height: 600px; 
    background-color: black; 
} 

这里是网页的HTML代码我的工作:

<DOCTYPE! html> 

<html> 

<head> 
<title> Purchase Games </title> 
<link rel="stylesheet" href="style.css"> 

<script> 

function submitData(){ 
    var firstname = document.forms["myForm"]["firstname"].value; 
    var lastname = document.forms["myForm"]["lastname"].value; 

    if(firstname == ""){ 
    alert("Please enter something as your first name."); 
     return; 
    } 
    if(lastname == ""){ 
    alert("Please enter something as your last name. "); 
    return; 
    } 
} 

function myFunction() { 
    document.getElementById("myDropdown").classList.toggle("show"); 
} 

window.onclick = function(event) { 
    if(!event.target.matches('.dropbtn')){ 
     var dropdowns = document.getElementsByClassName("dropdown-content"); 
     for(var i = 0; i < dropdowns.length; i++){ 
     var openDropdown = dropdowns[i]; 
     if(openDropdown.classList.contains('show')){ 
      openDropdown.classList.remove('show'); 
     } 
     } 
    } 
} 

</script> 
</head> 

<body> 
<div> 

<header><h1> Games Collection </h1></header> 

<nav> 

<ul> 
    <li><a href="index.htm"> Home </a></li> 
    <li><a href="casual.htm"> Casual </a></li> 
    <li><a href="shooter.htm"> Shooter </a></li> 
    <li><a href="platformer.htm"> Platformer </a></li> 
    <li><a href="purchase.htm"> Purchase Games </a></li> 
</ul> 

</nav> 

<article> 
    <h1> Purchase Games </h1> 
    <form name="myForm"> 
    First Name: <br> 
    <input type="text" name="firstname"> <br> <br> 
    Last Name <br> 
    <input type="text" name="lastname"> <br> <br> 

    <div class="dropdown"> 
    <button onclick="myFunction()" class="dropbtn"> Select Game Code </button> 
    <div id="myDropdown" class="dropdown-content"> 
    <a href="#"> FIFA </a> 
     <a href="#"> FORZA </a> 
    <a href="#"> POOL </a> 
    <a href="#"> MINE </a> 
    <a href="#"> PES </a> 
    <a href="#"> CODMW </a> 
     <a href="#"> TITAN </a> 
    <a href="#"> TITAN2 </a> 
    <a href="#"> BATTLE </a> 
    <a href="#"> PLANT </a> 
    <a href="#"> INSIDE </a> 
    <a href="#"> TERRA </a> 
    <a href="#"> MANY </a> 
    <a href="#"> MARIO </a> 
    <a href="#"> RYMAN </a> 
    </div> 
    </div> 
    <br> 
    <input type="submit" value="Confirm" class="confirmbtn" onclick="submitData()"> 
    <br> <br> 
    </form> 

    <textarea rows="10" cols="50" id="myTextArea"> 
    Once you purchase an item this is where it will appear 
    </textarea> 
</article> 

<footer><h3> Created by Michael Shepherd (Use this link: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_filter" </h3></footer> 

</div> 
</body> 

</html> 

回答

1

提交按钮的默认行为。所以,你需要特别所以加

<button type="button">Button</button> 

到您的按钮添加以下

HTML

<div> 

    <header> 
    <h1> Games Collection </h1></header> 

    <nav> 

    <ul> 
     <li><a href="index.htm"> Home </a></li> 
     <li><a href="casual.htm"> Casual </a></li> 
     <li><a href="shooter.htm"> Shooter </a></li> 
     <li><a href="platformer.htm"> Platformer </a></li> 
     <li><a href="purchase.htm"> Purchase Games </a></li> 
    </ul> 

    </nav> 

    <article> 
    <h1> Purchase Games </h1> 
    <form name="myForm"> 
     First Name: 
     <br> 
     <input type="text" name="firstname"> 
     <br> 
     <br> Last Name 
     <br> 
     <input type="text" name="lastname"> 
     <br> 
     <br> 

     <div class="dropdown"> 
     <button type="button" onclick="myFunction()" class="dropbtn"> Select Game Code </button> 
     <div id="myDropdown" class="dropdown-content"> 
      <a href="#"> FIFA </a> 
      <a href="#"> FORZA </a> 
      <a href="#"> POOL </a> 
      <a href="#"> MINE </a> 
      <a href="#"> PES </a> 
      <a href="#"> CODMW </a> 
      <a href="#"> TITAN </a> 
      <a href="#"> TITAN2 </a> 
      <a href="#"> BATTLE </a> 
      <a href="#"> PLANT </a> 
      <a href="#"> INSIDE </a> 
      <a href="#"> TERRA </a> 
      <a href="#"> MANY </a> 
      <a href="#"> MARIO </a> 
      <a href="#"> RYMAN </a> 
     </div> 
     </div> 
     <br> 
     <button type="submit" value="Confirm" class="confirmbtn" onclick="submitData()"></button> 
     <br> 
     <br> 
    </form> 

    <textarea rows="10" cols="50" id="myTextArea"> 
     Once you purchase an item this is where it will appear 
    </textarea> 
    </article> 

    <footer> 
    <h3> Created by Michael Shepherd (Use this link: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_filter" </h3></footer> 

,并在JSfiddle

链接

<button type="button" onclick="myFunction()" class="dropbtn"> Select Game Code </button> 

最终代码

只记得在html之前保留你的函数myFunction()。我看到你在头上。

-2
.dropdown-content 
{ 
    display : none: // remove this line 
} 
+0

我刚刚尝试过这一点,并停止下拉菜单从刚刚消失,但现在它只是保持不断打开即使我没有点击按钮 –

+0

使用选择而不是div ..并给所有的值在选项...它会正常工作.. –

相关问题