繁体   English   中英

HTML下拉菜单在单击时不会保持打开状态

[英]HTML drop down menu won't stay open when clicking on it

我正在使用html和CSS创建一个用于大学项目的网站,并且试图添加一个下拉菜单。 我遵循了来自w3Schools的教程,但是由于某些原因,当我单击按钮以打开菜单时,它不会保持打开状态并立即消失。 我已经尽可能地检查了我的代码,但看不到它出了什么问题。 谢谢

这是我的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>

按钮的默认行为是提交。 因此,您需要专门添加

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

所以在您的按钮上添加

<button type="button" onclick="myFunction()" class="dropbtn"> Select Game Code </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的链接

只要记住将函数myFunction()保留在html之前即可。 我看到你的头脑。

使用HTML中的“选择标签”获取多个值

请参考此链接。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM