javascript - 为什么我的菜单没有下拉菜单?

标签 javascript jquery css menu dropdown

我正在为我的网站制作一个“登录”/“注册”下拉菜单,但无法弄清楚为什么它不能正常工作。

我是 jQuery 的初学者,所以问题可能出在其中。或者它可能与 HTMl 或 CSS 有关,我不确定。当我测试页面并打开控制台时,jQuery 没有显示任何错误,所以我不确定问题出在哪里。这是代码:

HTML:

<div id="navthing">
      <h2><a href="#" id="loginform">Log in</a> | <a href="#">Sign Up</a></h2>
   <div class="login">
    <div class="arrow-up"></div>
     <div class="formholder">
       <div class="randompad">
           <fieldset>
             <label name="email">Email</label>
             <input type="email" value="example@example.com" />
             <label name="password">Password</label>
             <input type="password" />
             <input type="submit" value="Login" />
           </fieldset>
       </div>
      </div>
   </div>
</div>

CSS:

#navthing {
    text-align: right;
    padding: 0.5em;
}

.login {
    position: relative;
    width:250px;
    display:none;
}

.arrow-up {
  width: 0;
  height: 0;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 15px solid #ECF0F1;
  left: 10%;
  position: absolute;
  top: -10px;
}

.formholder input[type="email"], .formholder input[type="password"] {
  padding: 7px 5px;
  margin: 10px 0;
  width: 96%;
  display: block;
  font-size: 18px;
  border-radius: 5px;
  border: none;
  -webkit-transition: 0.3s linear;
  -moz-transition: 0.3s linear;
  -o-transition: 0.3s linear;
  transition: 0.3s linear;
}

.formholder input[type="email"]:focus, .formholder input[type="password"]:focus {
  outline: none;
  box-shadow: 0 0 1px 1px #1abc9c;
}
.formholder input[type="submit"] {
  background: #1abc9c;
  padding: 10px;
  font-size: 20px;
  display: block;
  width: 100%;
  border: none;
  color: #fff;
  border-radius: 5px;
}
.formholder input[type="submit"]:hover {
  background: #1bc6a4;
}

.randompad {
  padding: 10px;
}

.green {
  color: #1abc9c;
}

a {
  color: #ecf0f1;
  text-decoration: none;
}
a:hover {
  color: #1abc9c;
}

jQuery:

$('input[type="submit"]').mousedown(function(){
  $(this).css('background', '#2ecc71');
});
$('input[type="submit"]').mouseup(function(){
  $(this).css('background', '#1abc9c');
});

$('#loginform').click(function(){
  $('.login').fadeToggle('slow');
  $(this).toggleClass('green');
});



$(document).mouseup(function (e)
{
    var container = $(".login");

    if (!container.is(e.target) 
        && container.has(e.target).length === 0) 
    {
        container.hide();
        $('#loginform').removeClass('green');
    }
});

它显示正常,但是当我点击“登录”时没有下拉。怎么了?谢谢。

编辑 1:html 页面的头部包含这些文件:

<head>
<meta charset="utf-8">
{% load staticfiles %} 

<title>{% block title %}Index{% endblock %}</title>
{% load static from staticfiles %}
{% block style_base %}
<link href="{% static 'css/styles.css' %}" rel="stylesheet">
{% endblock %}
<meta name="description" content="{% block description %}{% endblock %}">
<link href='https://fonts.googleapis.com/css?family=Roboto+Mono:400,700,300' rel='stylesheet' type='text/css'>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="static/js/jquery.min.js" type="text/javascript"></script>
<script src="static/js/main.js" type="text/javascript"></script>
<script src="static/js/login3.js" type="text/javascript"></script>
</head>

这里有两个 jQuery 源,但我只尝试了一个,但没有结果。网站(在 main.js 中)也有一个 slider ,它使用 jQuery 并且工作正常。

当我加载页面时,会出现登录 |注册,但当我点击“登录”按钮时没有任何反应(没有下拉表单,什么也没有)。

最佳答案

在你的 jquery 代码的第一部分,我添加了这个并且它起作用了:

$( document ).ready(function() {

}); 

解释:https://api.jquery.com/ready/

关于javascript - 为什么我的菜单没有下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448461/

相关文章:

jquery - 使用 jQuery 删除并向按钮添加类,添加的新类将无法使用它的点击功能

html - 如何在悬停整个 anchor 时在 anchor 内制作悬停元素

css - 样式复选框和单选按钮

CSS: 伪元素 :before 和 :after 从原始元素继承宽度/高度

javascript - 在函数内部修改变量后,为什么变量未更改? -异步代码引用

javascript - 根据下拉菜单/表单值输入在网页上输出数据

javascript - 等待 Ajax 调用数组完成

javascript - 在 Angular 1.6 应用程序中注入(inject)服务

javascript - 使用 for..in 和 every 循环对元素进行排序

javascript - 如何在 jQuery 模板内进行 RegExp 替换?